urlManager 在 Yii 2.0 中不工作

2024-02-05

我正在尝试学习 yii 2.0,目前我正在使用basicyii 2.0 版本。第一步是配置 url。所以根据指南,我启用了mod_rewrite,使用检查它phpinfo()然后在中添加以下行components of 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

<?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' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // 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' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        ‘urlManager’ => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
    ],
    'params' => $params,
];

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

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;

你需要配置apache以及。作为Yii 官方指南 https://github.com/yiisoft/yii2/blob/master/docs/guide/start-installation.md#recommended-apache-configuration- 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...

您还可以创建一个.htaccess将文件放入您的 Web 目录中,内容如下:

RewriteEngine on

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

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

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

UPDATE

推荐的 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 配置 https://github.com/yiisoft/yii2/blob/master/docs/guide/start-installation.md#recommended-nginx-configuration-

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

urlManager 在 Yii 2.0 中不工作 的相关文章

  • 对对象数组进行排序

    我在使用 PHP 手册中的示例时遇到了问题 所以我想在这里问这个 我有一个对象数组 有没有办法根据对象的内容对其进行排序 例如我的数组是 Array 0 gt stdClass Object id gt 123 alias gt mike
  • 我可以从匿名 PL/SQL 块向 PHP 返回值吗?

    我正在使用 PHP 和 OCI8 执行匿名 Oracle PL SQL 代码块 有没有什么方法可以让我绑定一个变量并在块完成后获取其输出 就像我以类似的方式调用存储过程时一样 SQL declare something varchar2 I
  • 如何使用 php 处理传出 webhook (Slack)

    我已经配置了 Slack outgoing webhook 但我不确定如何处理 Slack 发送到我指定的 URL 的 HTTP POST 请求 工作流程是这样的 当有人向指定通道发送消息时 API 将向指定 URL 之一发送 HTTP P
  • 在这个页面中,悬停不起作用,我不知道为什么,而且页脚也没有占用 100% 宽度,即使我已经给了它

    我的编码是否不正确 或者悬停和页脚有什么问题 我需要知道 php 邮件程序是正确的还是我有错误 我无法找到错误 因为邮件功能在本地服务器上不起作用
  • 需要有关使用 PHP 在 mysql 数据库中插入逗号分隔数据的帮助

    数据库表中已有的演示数据 INSERT INTO csvtbl ID SKU Product Name Model Make Year From Year To VALUES 1 C2AZ 3B584 AR Power Steering P
  • PHP 中的异步数据库/服务调用:Gearman 与 pthreads

    在我们的 LAMP 站点上 我们遇到一些服务必须多次调用数据库才能提取数据的问题 通常在 PHP 中完成此操作的方式 至少我的经验 是串行的 这显然是低效的 我们可以通过使用缓存和聚合一些查询来缓解一些低效率的问题 但在某些情况下我们仍然需
  • Blueimp jQuery 文件上传,传递额外的表单数据

    我可以使用一些帮助 我已经设法使 blueimp jQuery 文件上传为我工作 但我仍然绝对是一个新手 我对 jQuery 等知之甚少 所以请尝试将其清晰明了地提供给我尽可能简单 我会尝试具体一点 好的 我想实现的是人们可以上传照片 并为
  • 在 PHP 中关闭 session.cookie_secure 的后果

    在安全连接下关闭 PHP 中的 session cookie secure 会带来哪些安全风险 我很想关闭此功能 因为我无法访问从 https 页面到 http 页面的会话数据 风险在于 cookie 数据是通过纯 HTTP 传输的 任何在
  • 如何使用 PHP 从 MySQL 查询中按升序对值进行排序?

    我使用以下 PHP 脚本从 MySQL 表中获取和更改数据 并将结果打印在 HTML 表中 我希望按升序对数据进行排序 utilization percentage变量 它是由创建的 total client time total avai
  • PHP 5 的 SQLite 编译设置是什么?

    SQLite 3 7 附带了新的预写日志记录 WAL http www sqlite org wal html并且有很多设置可以被配置 http www sqlite org compile html 但是 似乎没有任何方法可以改变任何事情
  • PDO fetch() 失败时会抛出异常吗?

    有没有方法PDO语句 fetch http php net manual en pdostatement fetch php如果 PDO 错误报告系统设置为抛出异常 则在失败时抛出异常 例如 如果我设置 PDO ATTR ERRMODE g
  • 在mysql中搜索“SanF”时获取旧金山的记录

    当我搜索 SanF 时获得 San Francisco 记录 SELECT FROM table WHERE col LIKE san Works SELECT FROM table WHERE col LIKE san F Works S
  • Zend Framework 2 将变量传递给模型

    我目前正在开发一个多语言网站 对于多语言部分 我使用翻译器 poedit 我将所选语言存储在会话中 效果很好 模块 php public function onBootstrap MvcEvent e session new Contain
  • 何时以及为何应使用 $_REQUEST 而不是 $_GET / $_POST / $_COOKIE?

    标题中的问题 当所有 3 个都发生时会发生什么 GET foo POST foo and COOKIE foo exist 其中哪一个被包含到 REQUEST 我想说永远不会 如果我想通过各种方法设置某些内容 我会为每个方法编写代码以提醒自
  • 为 SimpleXMLElements 数组编写 foreach 循环

    我正在使用 PHP 5 中的 XPath 来解析 XML 文档 我遇到的问题是写一个foreach正确显示以下数组 XML 文档示例 值1 值2 xmlfile link to file xml xmlRaw file get conten
  • 从命令行运行 PHP 脚本

    如何使用用于解析 Web 脚本的 PHP 解释器从命令行运行 PHP 脚本 我有一个phpinfo php从网络访问的文件显示German已安装 但是 如果我运行phpinfo php从命令行使用 php phpinfo php and g
  • 选择MySql表数据放入数组中

    我尝试从 mysql 捕获数据并将它们全部放入数组中 认为 users table id name code 1 gorge 2132 2 flix ksd02 3 jasmen skaod2 sql mysql query select
  • PHP邮件功能有时可以工作

    我正在编写一个脚本 需要通过PHP邮件功能发送电子邮件 如下所示 它在向 gmail 帐户发送电子邮件时有效 但在我的域中的帐户却无效 我们正在运行 Exchange 服务器 截至目前 电子邮件是从 www server 发送的 有谁知道
  • Laravel 集合 .each() + array_push

    需要有关 Laravel 上 each 方法内的 array push 的帮助 我无法在此代码上获取容器数组 imagesData array collect data images gt each function v k use ima
  • SQL 大表中的随机行(使用 where 子句)

    我有一个网站 人们可以在其中对汽车进行投票 向用户展示 4 辆汽车 他 她可以投票选出他们最喜欢的汽车 桌子cars有重要的列 car id int 10 not auto increment so has gaps views int 7

随机推荐