Home » PHP » Yii2 essay (six) modify the request triggered by e...

Yii2 essay (six) modify the request triggered by events.

yii2 can use the event mechanism to trigger the special treatment, the realization of the code expansion, can be understood as the yii2 kernel layer has buried the hidden "hooks" for later expansion, such as: run (yiibaseapplication:) method is buried under two "hook"

// yiibaseApplication
Public, function, run ()
$this->state = self:: STATE_BEFORE_REQUEST;
The first event (trigger / hook), modify some parameters in the executive body before hand
$this->trigger (self:: EVENT_BEFORE_REQUEST);
$this->state = self:: STATE_HANDLING_REQUEST;
$response = $this->handleRequest ($this->getRequest ());
Trigger / / second events ("hook"), the main trigger for running after some logic
$this->state = self:: STATE_AFTER_REQUEST;
$this->trigger (self:: EVENT_AFTER_REQUEST);
$this->state = self:: STATE_SENDING_RESPONSE;
$response->send ();
$this->state = self:: STATE_END;
Return $response->exitStatus;
} catch (ExitException, $e) {
$this->end ($e->statusCode, isset ($response), $response: null);
Return $e->statusCode;

We take advantage of the first event to trigger modified routing control for testing purposes only

1. adds configuration

in config/web.php

// adds a path alias that can be understood as registering a new package as SDK, and the package path as the specified folder
Yii:: setAlias (' sdk' dirname (__DIR__).' /sdk');

// registers a component named test and specifies the object corresponding to the component, and yii2 creates the object automatically using the createObject method. Other keys can represent attribute values of the test component (if any)
$config[' components'][' test' class' ['] = >; =>' sdkTest'];
//test components automatically loaded
$config[' bootstrap'][] = ' test'

2. create the SDK directory in the root directory (and the config sibling directory) and create Test.php in the SDK directory. The code is as follows:

Namespace sdk;
Use Yii;
/ / / / case, should be completely with the file name, the first letter is yii2 specification.
This is because with the succession of Object / yii2 operation mode, use init () function as the original code function, of course just do this matter needs, can be written in the constructor, without any base class inheritance
Class Test extends yiibaseObject{
/ / constructor operation
Public, function, init () {
A registration / / handle events in App, the corresponding events mentioned above: run (yiibaseApplication:) in the first event
Yii:: $app->on (yiibaseApplication:: EVENT_BEFORE_REQUEST, function ($event) {
Get / / request object
$request = Yii:: $app->getRequest ();
Check the object / source, find the object can modify the contents are set.... at the beginning, we use the setPathInfo (), set the routing controller (used to specify the controller analytical processing).
$request->setPathInfo (' message/pros');

PS: this function is used only as a test, so that all the paths are modified to point to the controller'message/pros'. after modification