urlManager 在 Yii 2.0 中不起作用

2022-01-04 00:00:00 url php yii2 yii mod-rewrite

我正在尝试学习 yii 2.0,目前我正在使用 yii 2.0 的 basic 版本.第一步是配置网址.因此,根据指南,我启用了 mod_rewrite,使用 phpinfo() 检查它,然后在 config 的 components 中添加以下几行/web.php:

I'm trying to learn yii 2.0 and currently i'm using basic version of yii 2.0. First step was to configure urls. So as per the guide, i enabled mod_rewrite, checked it using phpinfo() and then added following lines in the components of config/web.php:

‘urlManager’ => [
    'class' => 'yiiwebUrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
],

现在我希望 http://localhost/basic/web/index.php?r=site/testhttp://localhost/basic/web/index 一样工作.php/站点/测试

但它带我到SiteControllerindex 方法.实际上,它将所有 url 带到 index 方法.index.php 之后的部分无关紧要.即使是错误的 controllerId/actionId 也能工作.可能是什么问题?

But it takes me to the index method of SiteController. Actually it's taking all urls to index method. Part after index.php doesn't matters. Even a wrong controllerId/actionId works. What could be the problem?

这是我的config/web.php

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'tSjCFs0He7lBeZN34fLzFij2xUUE4NwK',
        ],
        'cache' => [
            'class' => 'yiicachingFileCache',
        ],
        'user' => [
            'identityClass' => 'appmodelsUser',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yiiswiftmailerMailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yiilogFileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        ‘urlManager’ => [
            'class' => 'yiiwebUrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yiidebugModule';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yiigiiModule';
}

return $config;

推荐答案

您还需要配置 apache.作为 Yii 的官方指南 说:

You need to configure apache as well. As Yii's official guide says:

推荐的 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...

您还可以在您的 Web 目录中创建一个 .htaccess 文件,其中包含以下内容:

You can also create a .htaccess file into your web directory with the following content:

RewriteEngine on

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

另一方面,您必须将 ` 更改为 ',如下所示:

On the other hand, you must change ` to ' like below:

'urlManager' => [ //you wrote `urlManager` which must change to 'urlManager'
        'class' => 'yiiwebUrlManager',
        '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;
}
}

推荐的 Nginx 配置

相关文章