tp5.0路由route.php,thinkphp5.1使用Route路由
一、開啟路由
thinkphp的路由一般默認都是開啟的,如果沒有開啟,可以在config.php里添加如下配置:
'url_route_on' => true, //開啟路由
'url_route_must' => true,//表示強制開啟路由,必須定義路由才能訪問。一般都是使用false,即混合模式,不用路由也可以訪問
二、添加路由規(guī)則
1、首先在application\index\controller\Index.php添加一個hello的方法,如下:
2、在route/route.php或者application\route.php添加路由規(guī)則,如下:
'index/hello',
];
3、定義路由的請求類型和后綴
return [
// 定義路由的請求類型和后綴
'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'do']],
];
定義后綴后,必須添加后綴才能訪問,如下:
三、添加路由別名
方式一:
動態(tài)方法:Route::alias('規(guī)則名稱','模塊/控制器',[路由參數]);
/*--------------------------------------------
* 添加路由別名:
* 1、別名路由到 分組/控制器名
* 2、瀏覽器訪問:http://域名/別名
* -------------------------------------------*/
Route::alias('index','index/Index');
Route::alias('login','index/Login');
Route::alias('main','index/Main');
/*--------------------------------------------
方式二:
動態(tài)數組:return[
'__alias__'=>['規(guī)則名稱','模塊/控制器',[路由參數]]
];
return [
//路由別名
'__alias__' => [
'index' => 'index/Index',
'login'=> 'index/Login',
'main'=> 'index/Main',
],
];
四、添加路由分組
1、在application\index\controller\Index.php添加如下幾個方法,如下:
2、在route/route.php或者application\route.php添加路由分組規(guī)則,如下:
[
':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、瀏覽器訪問,效果如下圖:
五、thinkphp訪問路由報錯“No input file specified.”的解決方法:
修改偽靜態(tài)配置文件.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]
總結
以上是生活随笔為你收集整理的tp5.0路由route.php,thinkphp5.1使用Route路由的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多信息源确认苹果今年将收窄 iPhone
- 下一篇: php框架所用到的核心概念,【PHP】P