查看和修改默認模塊名:
查看配置在 config/app.php 中:
// 默認應用 'default_app' => 'index',
項目默認安裝的默認模塊是 index,
可以自定義修改為其他模塊名,比如home, 只需要調整 配置項 default_app 值為home,把默認模塊名index目錄改名為home即可
獲取默認模塊名稱,代碼如下:
<?php //默認模塊名 $defalutModule = config('app.default_app'); //當前模塊名 $currentModule = app('http')->getName();
偽靜態設置隱藏默認模塊名:
比如默認模塊名是index:
原先的偽靜態規則:
if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; }
調整修改nginx配置為:
if (!-e $request_filename) { rewrite ^/admin(.*)$ /index.php?s=/admin$1; rewrite ^/mobile(.*)$ /index.php?s=/mobile$1; rewrite ^/api(.*)$ /index.php?s=/api$1; rewrite ^((?!/index).*)$ /index.php?s=/index$1; # hide url /index rewrite ^(.*)$ /index.php?s=$1 last; break; }
然后重啟nginx。
這樣原地址: http://www.test.com/index/Aboutus/about.html
可以縮短為:http://www.test.com/Aboutus/about.html
就去掉了網址中帶有 /index 的默認模塊名前綴。
自定義 siteurl() 函數,替換原 url() 函數方法:
/** * 修正url地址 * @param string $url 原地址 */ function fixurl($url = '') { $string = (string) $url; $hideHomeName = true; // 是否去除url中默認模塊名index $defalutModule = config('app.default_app'); $currentModule = app('http')->getName(); if($hideHomeName && $currentModule == $defalutModule) { # 去除url中默認模塊名index $search = '/'.$defalutModule.'/'; $pos = stripos($string, $search); if($pos !== false && substr_count(rtrim($string,'/'), '/') >= 2) { $string = substr($string, 0, $pos).'/'.substr($string, $pos + strlen($search)); } } return $string; } /** * Url生成 * @param string $url 路由地址 * @param array $vars 變量 * @param bool|string $suffix 生成的URL后綴 * @param bool|string $domain 域名 * return string */ function siteurl(string $url = '', array $vars = [], $suffix = true, $domain = false) { $defalutModule = config('app.default_app'); $currentModule = app('http')->getName(); $string = (string) url($url, $vars, $suffix, $domain); $hideHomeName = true; // 是否去除url中默認模塊名index/ if($hideHomeName && $currentModule == $defalutModule) { # 去除url中默認模塊名index $search = '/'.$defalutModule.'/'; $pos = stripos($string, $search); if($pos === 0) { //$string = '/'.ltrim($string, $search); //$string = '/'.substr($string, strlen($search)); $string = substr($string, 0, $pos). '/'. substr($string, $pos + strlen($search)); } } return $string; }
