thinkphp thinkphp6伪静态设置隐藏默认模块名和入口文件

查看和修改默认模块名:
查看配置在 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://test.com/index/Aboutus/about.html
可以缩短为:http://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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值