routing packet technology



Route grouping

Use the group method of the Route class to register and define some common routing settings for group routing Parameters, for example:

Route::group('blog', function () {
    Route::rule(':id', 'blog/read');
    Route::rule(':name', 'blog/read');
})->ext('html')->pattern(['id' => '\d+', 'name' => '\w+']);

Group routing supports all routing parameter settings. For specific parameter usage, please refer to the routing parameters chapter.

If it is only used to set some common routing parameters (also called virtual groups) for some routing rules, you can also use:

Route::group(function () {
    Route::rule('blog/:id', 'blog/read');
    Route::rule('blog/:name', 'blog/read');
})->ext('html')->pattern(['id' => '\d+', 'name' => '\w+']);

Routing groups support nesting, for example:

Route::group(function () {
    Route::group('blog', function () {
        Route::rule(':id', 'blog/read');
        Route::rule(':name', 'blog/read');
    });
})->ext('html')->pattern(['id' => '\d+', 'name' => '\w+']);

If nested grouping is used, the subgroup will inherit the parameters and variable rules of the parent group, and the parameters and variable rules defined in the final routing rule will take precedence.

You can use the prefix method to simplify the definition of the same routing address. For example, the following definition

Route::group('blog', function () {
    Route::get(':id', 'blog/read');
    Route::post(':id', 'blog/update');
    Route::delete(':id', 'blog/delete');
})->ext('html')->pattern(['id' => '\d+']);

can be simplified to

Route::group('blog', function () {
    Route::get(':id', 'read');
    Route::post(':id', 'update');
    Route::delete(':id', 'delete');
})->prefix('blog/')->ext('html')->pattern(['id' => '\d+']);

Route exact match

If you want all routes under a certain group to match exactly, you can use

Route::group('blog', function () {
    Route::get(':id', 'read');
    Route::post(':id', 'update');
    Route::delete(':id', 'delete');
})->completeMatch()->prefix('blog/')->ext('html')->pattern(['id' => '\d+']);

Delayed route resolution

Support Delayed route resolution, that is to say, the routing rules you define (mainly group routing and domain name routing rules) are not actually registered when loading the routing definition file, but are actually registered when the routing group or domain name is matched. Registration and resolution are performed to greatly improve the performance of route registration and resolution.

The default is to turn off delayed route parsing. You can set it in the routing configuration file:

// 开启路由延迟解析
'url_lazy_route'         => true,

After turning on delayed route parsing, if you need to generate a route decryption URL, you need to use the command line command.

php think optimize:route

To generate routing cache resolution.

Defining routes through routing groups or domain name routing can take advantage of delayed resolution.

Once the delayed resolution of routing is enabled, the defined domain name routes and group routes will be delayed and resolved. That is to say, routing rules will only be registered after the domain name or group is actually matched to avoid unnecessary registration and parsing overhead.

Routing rule merge analysis

Routing rules under the same routing group support merge analysis without traversing all routing rules under the routing group, which can be greatly improved Performance of route resolution.

The usage of separately enabling merge rule parsing for a certain group is as follows:

Route::group('user', function () {
    Route::rule('hello/:name','hello');
    Route::rule('think/:name','think');
})->mergeRuleRegex();

In this way, all routing rules under the group only need to be matched and checked once no matter how many are defined (actually only rendezvous and check routing rules that match the current request type).

The mergeRuleRegex method can only be used for routing groups or domain name routing (domain name routing is actually a special group).

Or set up global merge rules in the routing configuration file (valid for all groups)

// 开启路由合并解析
'route_rule_merge'	=> true,

Pass in additional parameters

Yes Unifiedly pass additional parameters to the group routing

Route::group('blog', [
    ':id'   => 'Blog/read',
    ':name' => 'Blog/read',
])->ext('html')
->pattern(['id' => '\d+'])
->append(['group_id' => 1]);

The above group routing uniformly passes in the group_id parameter, and the value of this parameter can be obtained through the param method of the Request class.