urlManager在Yii 2.0中不起作用

时间:2014-10-20 06:07:47

标签: php url mod-rewrite yii yii2

我正在尝试学习yii 2.0,目前我正在使用basic版本的yii 2.0。第一步是配置网址。因此,根据指南,我启用mod_rewrite,使用phpinfo()进行检查,然后在components的{​​{1}}中添加以下行:

config/web.php

现在我希望‘urlManager’ => [ 'class' => 'yii\web\UrlManager', 'enablePrettyUrl' => true, 'showScriptName' => false, ], 能够作为http://localhost/basic/web/index.php?r=site/test

工作

但是我需要使用http://localhost/basic/web/index.php/site/test index方法。实际上,它将所有网址都采用SiteController方法。 index之后的部分并不重要。即使是错误的index.php也可以。可能是什么问题?

这是我的controllerId/actionId

config/web.php

1 个答案:

答案 0 :(得分:8)

您还需要配置apache。正如Yii's official guide所说:

推荐的Apache配置

# Set document root to be "basic/web"
DocumentRoot "path/to/basic/web"

<Directory "path/to/basic/web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

# ...other settings...

您还可以使用以下内容在您的网站目录中创建.htaccess文件:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

另一方面,你必须改变`到&#39;如下所示:

'urlManager' => [ //you wrote `urlManager` which must change to 'urlManager'
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
    ],

<强>更新

推荐的Nginx配置

server {
charset utf-8;
client_max_body_size 128M;

listen 80; ## listen for ipv4
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

server_name mysite.local;
root        /path/to/basic/web;
index       index.php;

access_log  /path/to/basic/log/access.log main;
error_log   /path/to/basic/log/error.log;

location / {
    # Redirect everything that isn't a real file to index.php
    try_files $uri $uri/ /index.php?$args;
}

# uncomment to avoid processing of calls to non-existing static files by Yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
#    try_files $uri =404;
#}
#error_page 404 /404.html;

location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_pass   127.0.0.1:9000;
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    try_files $uri =404;
}

location ~ /\.(ht|svn|git) {
    deny all;
}
}

Recommended Nginx Configuration