赞
踩
一、开启路由
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']],
];
定义后缀后,必须添加后缀才能访问,如下:
三、添加路由别名
方式一:
动态方法: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、浏览器访问,效果如下图:
五、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]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。