当前位置:   article > 正文

tp5.0路由配置及thinkphp5.1使用Route路由说明

tp5.0路由route.php,thinkphp5.1使用Route路由

一、开启路由

thinkphp的路由一般默认都是开启的,如果没有开启,可以在config.php里添加如下配置:

'url_route_on' => true, //开启路由

'url_route_must' => true,//表示强制开启路由,必须定义路由才能访问。一般都是使用false,即混合模式,不用路由也可以访问

二、添加路由规则

1、首先在application\index\controller\Index.php添加一个hello的方法,如下:

2、在route/route.php或者application\route.php添加路由规则,如下:

'index/hello',

];

3、定义路由的请求类型和后缀

return [

// 定义路由的请求类型和后缀

'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'do']],

];

定义后缀后,必须添加后缀才能访问,如下:

d2e7606dfdaebaedb461c979eb44bf9d.png

三、添加路由别名

方式一:

动态方法:Route::alias('规则名称','模块/控制器',[路由参数]);

/*--------------------------------------------

* 添加路由别名:

* 1、别名路由到 分组/控制器名

* 2、浏览器访问:http://域名/别名

* -------------------------------------------*/

Route::alias('index','index/Index');

Route::alias('login','index/Login');

Route::alias('main','index/Main');

/*--------------------------------------------

方式二:

动态数组:return[

'__alias__'=>['规则名称','模块/控制器',[路由参数]]

];

return [

//路由别名

'__alias__' => [

'index' => 'index/Index',

'login'=> 'index/Login',

'main'=> 'index/Main',

],

];

四、添加路由分组

1、在application\index\controller\Index.php添加如下几个方法,如下:

2、在route/route.php或者application\route.php添加路由分组规则,如下:

[

':year/:month' => ['index/find', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],

':id' => ['index/get', ['method' => 'get'], ['id' => '\d+']],

':name' => ['index/read', ['method' => 'get'], ['name' => '\w+']],

],

];

3、浏览器访问,效果如下图:

013fde92661ec255b0450b64ef0a340f.png

五、thinkphp访问路由报错“No input file specified.”的解决方法:

修改伪静态配置文件.htaccess,打开htaccess文件,在index.php之后添加?(问号),如下:

Options +FollowSymlinks -Multiviews

RewriteEngine On

RewriteCond $1 !^(index.php|images|robots.txt)

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]

2245ffac4aa1653f2344c4a11c9ad822.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/351309
推荐阅读