Yii 路由 当使用多个模块时


Yii routing when using multiple modules

我们最近重新安装了生产服务器,在那里我们有一个运行多个模块的 Yii 项目。在重新安装之前,路由部分工作良好。我们多次检查 Nginx 配置是否存在问题,但一切看起来都不错,实际上我们从旧服务器复制了工作配置。

问题是,每个请求(在任何子域上)都会产生 CHttpException: Unable to resolve the request "default/site/index". 即使我们调用任何控制器或其他任何东西。

我打开了一个新的子域(没有指定的规则),只是为了测试,它也重定向到此错误。(即使我们在没有最后两条规则的情况下尝试过)

我们对 Yii 有以下路由规则:(print_r的结果)

[http://www.domain.dk/<controller:'w+>/<action:'w+>/<wid:'w+>] => default/<controller>/<action>
[http://www.domain.dk/<controller:'w+>/<action:'w+>] => default/<controller>/<action>
[http://www.domain.dk/] => default/site/index
[http://api.domain.dk/<controller:'w+>/<action:'w+>/<wid:'w+>] => api/<controller>/<action>
[http://api.domain.dk/<controller:'w+>/<action:'w+>] => api/<controller>/<action>
[http://api.domain.dk/] => api/
[http://admin.domain.dk/<controller:'w+>/<action:'w+>/<wid:'w+>] => admin/<controller>/<action>
[http://admin.domain.dk/<controller:'w+>/<action:'w+>] => admin/<controller>/<action>
[http://admin.domain.dk/] => admin/site/index
[https://facebook.domain.dk/<controller:'w+>/<action:'w+>/<wid:'w+>] => facebook/<controller>/<action>
[https://facebook.domain.dk/<controller:'w+>/<action:'w+>] => facebook/<controller>/<action>
[https://facebook.domain.dk/] => facebook/site/index
[http://domain.dk/site/page/view/<view:'w+>/] => default/site/page
[http://<module:'w+>.domain.dk/<controller:'w+>/<action:'w+>/<wid:'w+>] => <module>/<controller>/<action>
[http://<module:'w+>.domain.dk/<controller:'w+>/<action:'w+>] => <module>/<controller>/<action>

所有的Nginx配置看起来像这样:

server {
        listen 80;
        server_name api.domain.dk;
        access_log /log/api.domain.dk_access.log;
        error_log /log/api.domain.dk_error.log;
        root /path.to.project/public_html;
        include yii-rules.conf;
}

yii-rules.conf文件有共同的规则,例如:(我只复制可以有所作为的内容)

charset utf-8;
index index.php;
location / {
        index  index.html index.php;
        try_files $uri $uri/ /index.php?$args;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
location ~ '.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+'.php)(/.+)$;
        #let yii catch the calls to unexising PHP files
        set $fsn /index.php;
        if (-f $document_root$fastcgi_script_name){
                set $fsn $fastcgi_script_name;
        }
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;
        #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
}

我们不知道什么会导致此错误,可能只是一个我们没有注意到的小问题:''

此错误不会在我们的日志中生成任何内容(当然只有访问日志),并且 yii 访问日志只是写入,我们可以在浏览器中看到:"无法解决..."

我们应该如何更改我们的配置或规则列表以避免此错误?

得了!

这是一件小事,花了一天多的时间才找到,因为它没有显示任何错误:

我们有我们的主配置文件,我们使用一个"自定义"配置文件,特别是服务器或开发机器,它被 gitignore 。所以问题是,我们将 2 个配置与内置的 PHP array_merge合并。

CMap::mergeArray解决了我们的问题。

我们的其他配置和网络服务器很好。