新的Thinkphp5的路由功能很强大,完全可以自定义以满足自己的要求
ThinkPHP5.0的路由规则如下:
http://serverName/index.php/module/controller/action/param/value/...
我们不仅可以通过Apache的.htaccess配置文件在url中隐藏index.php
还可以通过以下自定义路由配置
隐藏控制名,以达到URL更简短的效果
你的route.php配置如下
<?php
/*
* @Author: huangyuan
* @Date: 2017-03-01 14:39:37
* @Last Modified by: huangyuan413026@163.com
* @Last Modified time: 2017-03-01 14:39:37
* @Description: 路由配置,在URL中隐藏模块名
*/
return [
//默认首页
''=>'index/index',
//未隐藏模块名 http://tp5.com/index/5
// 'index:name'=>['index/hello',['name'=>'\w+']],
//隐藏模块名 http://tp5.com/5
':name'=>['index/hello',['name'=>'\w+']],
// 路由分组
'[]'=>[
':id'=>['index/who',['id'=>'\d+']]
// ':name'=>['index/hello',['name'=>'\w+']],
]
];
application/index/controller/index.php
<?php
/*
* @Author: huangyuan
* @Date: 2017-03-01 14:39:11
* @Last Modified by: huangyuan413026@163.com
* @Last Modified time: 2017-03-01 14:39:34
*/
namespace app\index\controller;
class Index
{
public function index()
{
echo '<br>This is the index method';
}
public function who($id){
echo $id;
echo '<br>This is the who method';
}
public function hello($name){
echo $name;
echo '<br>This is the hello method';
}
}
index action
who
action
hello方法
通过模块访问则会进入index
action
参考:
ThinkPHP5.0完全开发手册 - 路由模式
ThinkPHP5 快速入门(二):URL和路由
ThinkPHP5.0完全开发手册-架构总览
ThinkPHP5.0完全开发手册-URL访问
From WizNote