[yii2]urlmanger

首先配置下nginx,确保可以不使用index.php来访问

server{
    listen      8082;
    server_name yii2.dev;
    access_log logs/yii2.access.log;
    error_log logs/yii2.error.log error;
    root /home/admin/web/nginx/html/basic/web/;
    location / {
            index  index.html index.php;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
    }

APP中配置config

'urlManager' => [
            'enablePrettyUrl' => true, 
            'enableStrictParsing' => false,
            'showScriptName' => false,
            'rules' => [
                '<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
            ],
        ]

其中KEY的说明

enablePrettyUrl
Whether to enable pretty URLs. 
Instead of putting all parameters in the query string part of a URL,
pretty URLs allow using path info to represent some of the parameters and can thus produce more user-friendly URLs,
such as "/news/Yii-is-released", instead of "/index.php?r=news/view&id=100".
enableStrictParsing
Whether to enable strict parsing. 
If strict parsing is enabled, the incoming requested URL must match at least one of the $rules in order to be treated as a valid request.
Otherwise, the path info part of the request will be treated as the requested route. This property is used only when $enablePrettyUrl is true.
showScriptName
Whether to show entry script name in the constructed URL. Defaults to true. This property is used only if $enablePrettyUrl is true.
rules
The rules for creating and parsing URLs when $enablePrettyUrl is true. This property is used only if $enablePrettyUrl is true.
Each element in the array is the configuration array for creating a single URL rule.
The configuration will be merged with $ruleConfig first before it is used for creating the rule object.
enableStrictParsing,showScriptName,rules 要生效必须设置enablePrettyUrl=true.
因此要使rules配置的规则生效,需要首先设置enablePrettyUrl=true.设置完成以后按照映射规则访问。
http://yii2.dev:8082/admin/default/1 其中Module:admin controller:default action:view id:1 符合规则:'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',



转载于:https://www.cnblogs.com/zaric/p/4030922.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值