Learning about SlimFrameworkforPHPv3 (2) yesterday I said that the local URL can be successfully passed in the index. in php, add the get (pattern, clouser) route to the specified processing class. after processing (stored in the database here), return the response in the browser.
The last question left yesterday is:Can I add my own key to the App container?, Not solved today.
What we do today is: add a middleware and record the logs after processing is complete.
Slim3.0 no longer adds an Application to every Middleware as Slim2 does. Therefore, it is difficult to obtain App data in Middleware, and the problem of adding a key to the container is not solved. So my idea is as follows:
1. APP class is only used to add middleware and start the router;
2. Register the router as the Moduel for related business processing;
3. Moduel contains classes that implement logging;
4. specify the corresponding class in Middleware.
In fact, I have another question: when is the middleware _ invoke () method executed. Tracking code, we found that when adding middleware, the code is as follows:
protected function addMiddleware(callable $callable) { echo 'add middle ware'; if ($this->middlewareLock) { throw new RuntimeException('Middleware can’t be added once the stack is dequeuing'); } if (is_null($this->stack)) { $this->seedMiddlewareStack(); } $next = $this->stack->top(); $this->stack[] = function (ServerRequestInterface $req, ResponseInterface $res) use ($callable, $next) { $result = call_user_func($callable, $req, $res, $next); if ($result instanceof ResponseInterface === false) { throw new UnexpectedValueException( 'Middleware must return instance of \Psr\Http\Message\ResponseInterface' ); } return $result; }; return $this; }
These two sentences are important in this code:
$ Next = $ this-> stack-> top ();
$ This-> stack [] = function ();
This stack [] is added to the middleware stack, but this function () is an important thing, that is, the so-calledDependency Injection.Interestingly, the entire function is not executed now. if you want to execute the middleware _ invoke () method. When is the execution performed? logically, it is executed in the current middleware order and called in the code, namely, app-> run. It calls the top element of the middle stack and has the following code:
public function callMiddlewareStack(ServerRequestInterface $req, ResponseInterface $res) { if (is_null($this->stack)) { $this->seedMiddlewareStack(); } /** @var callable $start */ $start = $this->stack->top(); $this->middlewareLock = true; $resp = $start($req, $res); $this->middlewareLock = false; return $resp; }
At $ start ($ req, $ res), the anonymous function is executed, and the custom middleware _ invoke () is executed.
Here it should beDependency InjectionBut I still don't quite understand the concept. I need to study it later.
This is a bit of understanding of the source code.
Based on the above four ideas, there are two points in the design:
1. take the Module as the main body, and add required lib such as Log and DB to the module;
2. set the Module in Midllerware and call the Module method, such as writing logs.
Adding a Log to the Module makes DB easy to use. However, adding a Module to Middleware cannot be used because Middleware is registered before the module and the module associated with the Module during registration is NULL, the module cannot be found during subsequent execution. the methods do not have errors.
$app->add(Example\MiddleWare\MyMiddleware::instance());$app->post('/homepage', function ($request, $response, $args) use ($app) { Example\Module\Replace::instance($request,$response)->excute(); return $response;});
In this case, let the Module exist first, that is, instantiate the module. then, specify the module when registering middleware, so that no error is reported. modify the code for the Module and Middleware as follows:
/**** Module**/public function init($request,$response){ $this->request = $request; $this->response = $response; $this->excute();}public abstract function excute();
/*** Middleware*/ self::$middleWare = is_null(self::$middleWare)?new MyMiddleware($module):self::$middleWare; return self::$middleWare;}public function __construct($module){ $this->module = $module;}
Then add middleware and router in index. php. the code is as follows:
$repModule = Example\Module\Replace::instance();$app->add(Example\MiddleWare\MyMiddleware::instance($repModule));$app->post('/homepage', function ($request, $response, $args) use ($repModule) { $repModule->init($request,$response); return $response;});
In this way, the LogWrite function is implemented in Middleware, but the problem arises. here, the Module and Middleware are specified too early. if you cannot dynamically change the Module when registering other router. This is not good!
Is there any better way? there are many anonymous functions in the framework (Clouser ). Is there a Clouser solution?
The problem dynamically specified by the Module in Middleware needs to be solved. Or you can use the App to do something.