我正在尝试学习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
答案 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;
}
}