thinkPHP defined routing

URL address inside the indexmodule how it can be omitted, the default URL address seemed a bit long, the following is said on how to simplify access by URL routing.

We routing definition file ( application/route.php) inside add some routing rules, as follows:

return [
    // 添加路由规则 路由到 index控制器的hello操作方法
    'hello/:name' => 'index/index/hello', ]; 

The routing rules represent all hellothe beginning and access parameters are routed to indexthe controller of the hellooperation method.

URL address of the previous access routes are:

http://tp5.com/index/index/hello/name/thinkphp 

After defining a route can only access the following URL address

http://tp5.com/hello/thinkphp 

note


After you define routing rules, the original URL address will fail, become illegal request.

But there is a small problem, if we only access

http://tp5.com/hello 

Error occurs,

This is due to the fact that the route is not properly matched, we modify the routing rules are as follows:

return [
    // 路由参数name为可选
    'hello/[:name]' => 'index/hello', ]; 

Use []the routing rules variable wrap, it means that the variable is optional, then it can be a normal visit.

http://tp5.com/hello 

When namethe time value of the parameter is not passed, hellothe method nameparameter has a default value World, so the content is output Hello,World!

In addition to defining a route profile, you may also be employed dynamically defined routing rules define, for example, in the routing configuration file ( application/route.phpadded at the beginning of the following direct method) of:

use think\Route;

Route::rule('hello/:name', 'index/hello'); 

And the effects accomplished by using the same configuration is defined.

Whether or configuration method defined by the route of the Route class, are unified into the routing configuration file application/route.phpfile, the specific reasons will be announced later.

prompt:


Note the routing configuration settings are not supported in the module configuration file.

Exact hits

Routing is defined earlier as long as the match can start with hello, if you want full match, you can use the following definitions:

return [
    // 路由参数name为可选
    'hello/[:name]$' => 'index/hello', ]; 

When routing rules $when it means the end of the current routing rules require a full match.

When we visit the following URL address of the time:

http://tp5.com/hello // 正确匹配 http://tp5.com/hello/thinkphp // 正确匹配 http://tp5.com/hello/thinkphp/val/value // 不会匹配 

Closure defined

Also supports the closure by defining some special scenarios define routing rules, for example:

return [
    // 定义闭包
    'hello/[:name]' => function ($name) { return 'Hello,' . $name . '!'; }, ]; 

or

use think\Route;

Route::rule('hello/:name', function ($name) { return 'Hello,' . $name . '!'; }); 

prompt:


Variable parameter is the closure function of routing rules defined.

Therefore, when accessing the following URL address:

http://tp5.com/hello/thinkphp 

Will output

Hello,thinkphp!

Set URL delimiters

如果需要改变URL地址中的pathinfo参数分隔符,只需要在应用配置文件(application/config.php)中设置:

// 设置pathinfo分隔符
'pathinfo_depr'          => '-', 

路由规则定义无需做任何改变,我们就可以访问下面的地址:

http://tp5.com/hello-thinkphp 

路由参数

我们还可以约束路由规则的请求类型或者URL后缀之类的条件,例如:

return [
    // 定义路由的请求类型和后缀
    'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'html']], ]; 

上面定义的路由规则限制了必须是get请求,而且后缀必须是html的,所以下面的访问地址:

http://tp5.com/hello // 无效 http://tp5.com/hello.html // 有效 http://tp5.com/hello/thinkphp // 无效 http://tp5.com/hello/thinkphp.html // 有效 

更多的路由参数请参考完全开发手册的路由参数一节。

变量规则

接下来,我们来尝试一些复杂的路由规则定义满足不同的路由变量。在此之前,首先增加一个控制器类如下:

<?php

namespace app\index\controller;

class Blog
{ public function get($id) { return '查看id=' . $id . '的内容'; } public function read($name) { return '查看name=' . $name . '的内容'; } public function archive($year, $month) { return '查看' . $year . '/' . $month . '的归档内容'; } } 

添加如下路由规则:

return [
    'blog/:year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']], 'blog/:id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']], 'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']], ]; 

在上面的路由规则中,我们对变量进行的规则约束,变量规则使用正则表达式进行定义。

我们看下几种URL访问的情况

// 访问id为5的内容
http://tp5.com/blog/5 // 访问name为thinkphp的内容 http://tp5.com/blog/thinkphp // 访问2015年5月的归档内容 http://tp5.com/blog/2015/05 

路由分组

上面的三个路由规则由于都是blog打头,所以我们可以做如下的简化:

return [
    '[blog]' => [ ':year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']], ':id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']], ':name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']], ], ]; 

对于这种定义方式,我们称之为路由分组,路由分组一定程度上可以提高路由检测的效率。

复杂路由

有时候,我们还需要对URL做一些特殊的定制,例如如果要同时支持下面的访问地址

http://tp5.com/blog/thinkphp http://tp5.com/blog-2015-05 

我们只要稍微改变路由定义规则即可:

return [
    'blog/:id'            => ['blog/get', ['method' => 'get'], ['id' => '\d+']], 'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']], 'blog-<year>-<month>' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']], ]; 

对 blog-<year>-<month> 这样的非正常规范,我们需要使用<变量名>这样的变量定义方式,而不是 :变量名方式。

简单起见,我们还可以把变量规则统一定义,例如:

return [
    // 全局变量规则定义
    '__pattern__'         => [ 'name' => '\w+', 'id' => '\d+', 'year' => '\d{4}', 'month' => '\d{2}', ], // 路由规则定义 'blog/:id' => 'blog/get', 'blog/:name' => 'blog/read', 'blog-<year>-<month>' => 'blog/archive', ]; 

__pattern__中定义的变量规则我们称之为全局变量规则,在路由规则里面定义的变量规则我们称之为局部变量规则,如果一个变量同时定义了全局规则和局部规则的话,当前的局部规则会覆盖全局规则的,例如:

return [
    // 全局变量规则
    '__pattern__'         => [ 'name' => '\w+', 'id' => '\d+', 'year' => '\d{4}', 'month' => '\d{2}', ], 'blog/:id' => 'blog/get', // 定义了局部变量规则 'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w{5,}']], 'blog-<year>-<month>' => 'blog/archive', ]; 

【 5.1 】使用须知


5.1的路由配置文件改为route/route.php,并且支持随意命名,都会自动加载。并尽量使用方法注册路由的方式替代数组配置的方式,例如。

use think\facade\Route;

Route::get('blog/:id','blog/get'); Route::get('blog/:name','blog/read');

Guess you like

Origin www.cnblogs.com/xu1115/p/10977620.html