# LightPHP
**Repository Path**: fm100/LightPHP
## Basic Information
- **Project Name**: LightPHP
- **Description**: 我的第一个微服务框架实践
- **Primary Language**: PHP
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2018-07-27
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
##这是什么
一个框架,不完美,基于注解的路由器,依赖注入,优雅的中间件,然后没了。
##如何使用
写一个类文件,放到可以被框架自动加载的地方:
```php
/*
* @route('hello')
*/
class IndexController{
/**
* @route('world')
* @method('GET')
* @middleware('another')
*/
public function index(Request $request){
echo "Hello World!";
echo '
';
}
}
```
在`App\config.php`定义好该类```
```php
'apiProvider' => [
'App\Controller\IndexController',
],
```
访问`http://localhost/hello/world`你将看到`Hello World!`
就是这么简单!
##发生了什么?
聪明的你应该已经看到了,框架通过`注释获取路由信息`来实现输入输出
##需要中间件?
####1. 定义中间件
写一个中间件类,实现`Light\Contracts\Middleware`接口:
```php
class TestMiddleware implements Middleware{
public function handler(Request $request,Closure $next){
echo "Hello world by test middleware";
echo '
';
return $next($request);
}
}
```
再来一个吧
```php
class AnotherMiddleware implements Middleware{
public function handler(Request $request,\Closure $next){
echo "Hello world by another niddleware
";
return $next($request);
}
}
```
然后写一个接口类:
```php
/*
* @route('hello')
* @middleware('test')
*/
class IndexController{
/*
* @route('world')
* @method('GET')
* @middleware('another')
*/
public function index(Request $request){
echo "Hello World by controller";
echo '
';
}
}
```
在`App\config.php`定义好该中间件
```php
'middlewareProvider' => [
'test' => 'App\Middleware\TestMiddleware',
'another' => 'App\Middleware\AnotherMiddleware'
]
```
访问`http://localhost/hello/world`你将会看到:
```
Hello world by middleware
by another
Hello World by controller
```
就是这么简单!