simple-swoole / simps

🚀 A simple, lightweight and high-performance PHP coroutine framework.
https://simps.io
Apache License 2.0
471 stars 48 forks source link

add middleware support for default Route object #21

Closed inspurhua closed 4 years ago

inspurhua commented 4 years ago

function auth($handler){ return function($request, $response, $vars) use ($handler) { // do some buissiness logic return $handler($request, $response, $vars); }; }

sy-records commented 4 years ago

看了一下,你这个功能没实现完整啊?

inspurhua commented 4 years ago

看了一下,你这个功能没实现完整啊?

只加上了“类@方法”这种形式的路由参数,这个框架很好用,对于is_callable的,array形式的,我还不会处理。

sy-records commented 4 years ago

好吧,你这是个方法级别的中间件处理

sy-records commented 4 years ago
class IndexController
{
    public $middleware = ['index' => [__CLASS__ . '::test']];

    public static function test($handler)
    {
        return function ($request, $response, $vars) use ($handler) {
            // do something
            return $handler($request, $response, $vars);
        };
    }

    public function index($request, $response)
    {
        $response->end(
            json_encode(
                [
                    'method' => $request->server['request_method'],
                    'message' => 'Hello Simps.'
                ]
            )
        );
    }
}