How the framework routes are defined


Routing is the technology of locating the server resources to be accessed!


## Register route

The most basic route definition method is: `Route::rule('Routing expression', 'Routing address', 'Request type')`

use think\facade\Route;

Take a single application as an example:

// 注册路由到News控制器的read操作
Route::rule('new/:id','News/read');

When we visit:

http://serverName/new/5

, it will be automatically routed to:

http://serverName/news/read/id/5

and the original access address will automatically become invalid.

You can specify the request type in the rule method (if not specified, any request type will be valid by default), for example:

Route::rule('new/:id', 'News/update', 'POST');

The request type parameters are not case-sensitive.

indicates that the defined routing rules are only valid under POST requests. If you want to define the routing rules supported by GET and POST requests, you can use:

Route::rule('new/:id','News/read','GET|POST');

However, we usually recommend using the shortcut method corresponding to the request type, including:


TypeDescriptionShortcut methodGETGET requestgetPOSTPOST requestpostPUTPUT requestputDELETEDELETE requestdeletePATCHPATCH requestpatch*Any request typeany


The usage of the shortcut registration method is:

Route::shortcut method name ('routing expression', 'routing address')

Use An example is as follows:

Route::get('new/<id>','News/read'); // 定义GET请求路由规则
Route::post('new/<id>','News/update'); // 定义POST请求路由规则
Route::put('new/:id','News/update'); // 定义PUT请求路由规则
Route::delete('new/:id','News/delete'); // 定义DELETE请求路由规则
Route::any('new/:id','News/read'); // 所有请求都支持的路由规则

After registering multiple routing rules, the system will sequentially traverse the registered routing rules that meet the request type. Once the correct routing rule is matched, the final scheduling method will be executed, and subsequent rules will be executed. No more testing.

Rule expressions

Rule expressions usually include static rules and dynamic rules, as well as a combination of the two rules. For example, the following are all valid rule expressions Formula:

Route::rule('/', 'index'); // 首页访问路由
Route::rule('my', 'Member/myinfo'); // 静态地址路由
Route::rule('blog/:id', 'Blog/read'); // 静态地址和动态地址结合
Route::rule('new/:year/:month/:day', 'News/read'); // 静态地址和动态地址结合
Route::rule(':user/:blog_id', 'Blog/read'); // 全动态地址

The definition of the regular expression uses / as the parameter separator (no matter what your PATH_INFO separator setting is, please make sure to use / when defining the routing rule expression to separate the URL parameters, unless is the case of using combined variables).

Each parameter can include dynamic variables, for example: variables or <variables> both represent dynamic variables (the new version recommends using the second method, which is more conducive to mixed variable definitions), and will be automatically bound to The corresponding parameters of the operation method.

Your URL access PATH_INFO delimiter is configured using pathinfo_depr, but no matter how configured, it does not affect the routing delimiter definition of the routing rule expression.

Optional variables

Supports optional definition of routing parameters, for example:

Route::get('blog/:year/[:month]','Blog/archive');
// 或者
Route::get('blog/<year>/<month?>','Blog/archive');

After the variable is included with [ ] Indicates that this variable is an optional variable for route matching.

After defining the routing rules above, the following URL access addresses can be matched by the correct route:

http://serverName/index.php/blog/2015
http://serverName/index.php/blog/2015/12

After using optional variable definition, two or more routing rules need to be defined before. Handled situations can be combined into a routing rule.

Optional parameters can only be placed at the end of routing rules. If optional parameters are used in the middle, subsequent variables will become optional parameters.

Exact match

When rule matching is detected, the default is to only match the URL from the beginning. As long as the beginning of the URL address contains the defined routing rule, the match will be successful. , if you want the URL to match exactly, you can use the $ symbol at the end of the routing expression, for example:

Route::get('new/:cate$', 'News/category');

After this definition,

http://serverName/index.php/new/info

will match successfully, while

http://serverName/index.php/new/info/2

will Will not match successfully.

If it is defined in the

Route::get('new/:cate', 'News/category');

way, both URL access methods can be matched successfully.

If you need to completely match the URL globally, you can set

// 开启路由完全匹配
'route_complete_match'   => true,

Extra parameters in the routing configuration file

In the routing jump Sometimes it supports additional incoming parameter pairs (extra parameters refer to parameters that are not in the URL. Implicitly passing in required operations can sometimes play a certain role in security protection, which we will mention later). For example:

Route::get('blog/:id','blog/read')
    ->append(['status' => 1, 'app_id' =>5]);

In the above routing rule definition, the status and app_id parameters do not exist in the URL, and are implicit value transfers. Of course, they are not necessarily needed, but can be used when needed. Different additional parameters can be set for different routes.

If there is a conflict between the variables in the append method and the routing rules, the one passed in the append method will take precedence.

RouteIdentification

If you need to quickly generate a URL address based on the route, you can specify the generation identification when defining the route (but make sure only).

For example

// 注册路由到News控制器的read操作
Route::rule('new/:id','News/read')
    ->name('new_read');

You can use it when generating a routing address

url('new_read', ['id' => 10]);

If you do not define a routing identifier, the system will use the routing address as the routing identifier by default, for example You can use the following method to generate

url('News/read', ['id' => 10]);

Forced routing
Set

'url_route_must'		=>  true,

The routing rules for the homepage in the routing configuration file can be adopted/defined, for example, the following Website homepage routing output Hello, world!

Route::get('/', function () {
    return 'Hello,world!';
})