新的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

參考: