Domain name routing


Domain name routing

You can set routing rules for domain names separately, for example, register separate routing rules for blog subdomains:

Route::domain('blog', function () {
    // 动态注册域名的路由规则
    Route::rule('new/:id', 'news/read');
    Route::rule(':user', 'user/info');
});

Once a domain name route is defined, access to the domain name will only read the routing rules defined by the domain name route.

Other methods of routing can be used in closures, including routing groups, but domain name routing cannot be included

Supports setting the same routing rules for multiple domain names at the same time:

Route::domain(['blog', 'admin'], function () {
    // 动态注册域名的路由规则
    Route::rule('new/:id', 'news/read');
    Route::rule(':user', 'user/info');
});

If you need to set a route that can take effect across all domain names, you can use the crossDomainRule method for group routing or a certain route:

Route::group( function () {
    // 动态注册域名的路由规则
    Route::rule('new/:id', 'news/read');
    Route::rule(':user', 'user/info');
})->crossDomainRule();

Domain name binding

Bind to controller class

// blog子域名绑定控制器
Route::domain('blog', '@blog');

Bind to namespace

// blog子域名绑定命名空间
Route::domain('blog', ':\app\blog\controller');

Bind to class

// blog子域名绑定到类
Route::domain('blog', '\app\blog\controller\Article');

Pan-domain name deployment

Can support pan-domain name deployment rules, for example:

// 绑定泛二级域名域名到book应用
Route::domain('*', 'book');

The following URL access will directly access the book application

http://hello.thinkphp.cn
http://quickstart.thinkphp.cn

And you can get the current pan-domain name value directly through Request::panDomain().

Supports third-level pan-domain name deployment, for example:

// 绑定泛三级域名到user应用
Route::domain('*.user', 'user');

also supports directly passing the value of the pan-domain name as an additional parameter

// 绑定泛三级域名到user应用
Route::domain('*.user', 'user?name=*');

through Request::param( 'name') Gets the value of the current pan-domain name

Currently only supports the deployment of pan-domain names for second-level domain names and third-level domain names.

Bind to the Response object

You can directly bind a domain name to the Response object, for example:

// 绑定域名到Response对象
Route::domain('test', response()->code(404));

If the domain name needs to be Define routing rules and perform binding operations for other situations. You can perform binding operations in closures. For example:

Route::domain('blog', function () {
    // 动态注册域名的路由规则
    Route::rule('new/:id', 'index/news/read');
})->bind('blog');

defines a new/:id routing rule under the blog domain name, pointing to index application, while other routes are bound to the blog application.

Pass in additional parameters

You can pass in additional hidden parameters after domain name binding or routing definition, for example:

Route::domain('blog', function () {
    // 动态注册域名的路由规则
    Route::rule('new/:id', 'news/read');
    Route::rule(':user', 'user/info');
})->append(['app_id'=>1]);

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

You can also directly pass in additional parameters after binding the domain name

Route::domain('blog', 'blog')
	->append(['app_id'=>1]);

Routing parameters

The domain name route itself is also a routing group. Therefore, public routing parameters can be defined in the same way as routing groups, for example:

Route::domain('blog', function () {
    // 动态注册域名的路由规则
    Route::rule('new/:id', 'news/read');
    Route::rule(':user', 'user/info');
})->ext('html')
->pattern(['id' => '\d+'])
->append(['group_id' => 1]);