thinkphp5在URL地址里隱藏模塊名


新的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
參考:
 
 
 
 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM