Yii2的urlManager URL美化


 

 

Yii1.*與Yii2中配置路由規則rules是幾乎是一樣的,但還是有細微的差別。

 

在Yii1.*中開啟path路由規則直接使用

'urlFormat' => 'path',

但在Yii2中已經沒有urlFormat 對象方法,在Yii2取而代之的是

 'enablePrettyUrl'=>TRUE,

 

一個典型的Yii1.* urlManager配置:

  'urlManager' => array(
            'urlFormat' => 'path',
            'showScriptName' => false, //隱藏index.php  
            'urlSuffix' => '.html', //后綴   
            'rules' => array(
                'news/detail-<id:.*>' => 'news/detail', //http://cms.com/news/detail-27d24c26.0486c.0aea8.a3d2803b.0000111.03.html   id==>[id] => 27d24c26.0486c.0aea8.a3d2803b.0000111.03
                'news/<id:.*>-<category:\d+>' => 'news', //http://cms.com/news/02f5bc8f-04600-0b477-c6bc82ab-0000111-03-1.html ==== $this->createUrl('news/', array('id' => $value['id'],'category'=>1));  
                'singlePage/<id:.*>' => 'singlePage',  
                'contact' => 'about/contact',  
                'addOrder' => 'Online/addOrder',  
                /**
                 * $this->createUrl('news/index',array('userid'=>123,'title'=>3434,'nid'=>'sdfsdfsdf'))   index.php/new/index?userid=123&title=3434&nid=sdfsdfsdfsd
                 * http://cms.com/news/123/3434/sdfsdfsdf-index.html      
                 */
               'news/<id:\d+>/<title:.*?>/<nid:.*?>-index' => 'news/index',
                'refresh/' => 'index/Refresh',
                'index.jsp/' => 'index/',
                'index.aspx/' => 'index/',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ),

 

那Yii2如何配置呢?

首先我們的URL地址是這樣的

http://new.com/index.php?r=auth%2Fmenulink&post=2

我們要讓地址改為path模式:

http://new.com/auth/menulink/post/2

1.在Nginx中開啟rewrite 

server {
        listen       80;
        server_name  new.com ;
        location / {
            root   F:/www/new/web;
            index  index.html index.htm index.php;
            #autoindex  on;
             if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }
        location ~ \.php$ {
            root          F:/www/new/web;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

 

2.config中web.php 配置

 'urlManager' => [
            'enablePrettyUrl' => true,  //美化url==ture
            'enableStrictParsing' => false,  //不啟用嚴格解析 
            'showScriptName' => false,   //隱藏index.php
            'rules' => [
                '<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                 '/m-api/' => '/meanpan-api/index', // 默認index
                 '/mp-api/<action>' => '/meanpan-api/<action>',   // /mp-api/*==>/meanpan-api/*
            ],
        ]

 

 

參數說明:

Yii官網說明:http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html

 

$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.

 

現在訪問URL就成為path模式了。

 

 

 

啟用開發模式DEBUG

 if (YII_ENV_DEV) {
    // 啟用開發模式DEBUG
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug']['class'] = 'yii\debug\Module';
    $config['modules']['debug']['allowedIPs'] =['127.0.0.1','201.104.104.104',"::1"];
    $config['modules']['debug']['historySize'] =500;  # 修改debug記錄條數

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] ['class']= 'yii\gii\Module';
    $config['modules']['gii']['allowedIPs'] =['127.0.0.1','201.104.104.104',"::1"];
}

 


免責聲明!

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



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