欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

YII2 路由管理

程序员文章站 2024-02-14 12:56:10
...

URL美化分两个阶段完成:web服务器配置、应用配置
在web服务器中配置多域名站点配置,为保证项目安全,在YII目录下的web目录作为项目发布的目录,其中index.php作为统一入口文件。
为了美化URL,配置web服务器重写规则,取消index.php。不过前提条件是web服务器必须开启重写模块(rewrite)。
为YII配置web服务器,让所有用户请求都发送给入口脚本index.php来处理。由此框架需提供路由和URL管理两项功能。路由用于标识处理用户请求的module、controller、action三部分。而对于简化的url有利于SEO优化,采用pathinfo模式是常用方式,但是nginx服务器本身并不支持此模式。
对于用户请求的URL地址,一般分为四部分:主机信息、入口脚本、路由信息、查询参数。

Apache

1.在apache的httpd.conf配置文件中打开rewrite模块
2.在web目录下新建.htaccess文件

Options +FollowSymLinks
    IndexIgnore */*
    RewriteEngine on

    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # otherwise forward it to index.php
    RewriteRule . index.php

3.在应用主配置文件main.php的组件部分加入

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

Nginx

1.LNMP环境下支持pathinfo并隐藏index.php

//多域名配置文件
vim /usr/local/nginx/conf/vhost/dsn.cn
server
    {
        listen 80;
        #listen [::]:80;
        server_name adm.yii.cn ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/yii.cn/backend/web/;

        include other.conf;
        #error_page   404   /404.html;

        //隐藏index.php
        location /
        {
            if (!-e $request_filename)
            {
                rewrite "^/(.*)$" /index.php last;
            }
        }
        
        //配置nginx支持pathinfo模式
        include enable-php-pathinfo.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }
        

        access_log  /home/wwwlogs/adm.cn.log;

//重新nginx配置
/etc/init.d/nginx reload

2.应用中配置UrlManager组件
/backend/config/main.php

'components'=>[
        //URL美化配置
        'urlManager' => [
            //是否开启美化
            'enablePrettyUrl' => true,
            //是否显示统一入口脚本index.php
            'showScriptName' => false,
            //伪静态文件后缀
            'suffix'=>'.html',
            //美化规则
            'rules' => [
            ],
        ],
]

3.设置美化规则

'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
前:http://adm.yii.cn/article/view.html?id=100
后:http://adm.yii.cn/article/view/100.html
'<controller:\w+>/<id:\d+>' => '<controller>/view',
前:http://adm.yii.cn/article/view.html?id=100
后:http://adm.yii.cn/article/100.html
'<controller:\w+>/<id:\d+>/<action:\w+>' => '<controller>/<action>',
前:http://adm.yii.cn/article/view.html?id=100
后:http://adm.yii.cn/article/100/view.html

4.LNMP环境配置
Nginx中修改多域名配置文件加入重写规则

//修改nginx多域名配置
vim /usr/local/nginx/conf/vhost/dsn.conf

//方式1
location / {
  //-e表示若请求文件存在(exists)
  if(!-e $request_filename){
    rwrite ^/(.*)$/index.php/$1 last;
  }
}

//方式2
location / {
  //-f表示若请求的文件为常规文件(file)
  if(!-f $request_filename){
    rewrite (.*)/index.php;
  }
}

//重新加载配置
service nginx reload

//添加本地域名映射
vim /etc/hosts
127.0.0.1 dsn.cn