thinkphp5在URL地址里隐藏模块名
新的Thinkphp5的路由功能很强大,完全可以自定义以满足自己的要求
ThinkPHP5.0的路由规则如下:http://serverName/index.php/module/controller/action/param/value/...
![](https://images2015.cnblogs.com/blog/69929/201703/69929-20170301152539266-1102582192.png)
我们不仅可以通过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
![](https://images2015.cnblogs.com/blog/69929/201703/69929-20170301145421095-127886948.png)
who action
![](https://images2015.cnblogs.com/blog/69929/201703/69929-20170301145421688-1047413784.png)
hello方法
![](https://images2015.cnblogs.com/blog/69929/201703/69929-20170301145422345-333355096.png)
通过模块访问则会进入index action
![](https://images2015.cnblogs.com/blog/69929/201703/69929-20170301145423001-1252074418.png)
参考: