【问题标题】:Yii 2.0 restful routing 404 issueYii 2.0 restful 路由 404 问题
【发布时间】:2015-01-04 07:26:14
【问题描述】:

我正在尝试使用我的简单产品表来遵循此处找到的 Yii2.0 示例 而不是用户。

http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

我使用的是基本包,不是高级包。

当我尝试访问 localhost/product 我收到 404 错误。

当我使用 localhost/index.php 或 localhost/index.php/gii 我得到了预期的结果(默认主页和 gii 工具)。

这是我正在使用的。

配置文件 web.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' => 'xxx',
        'parsers' => [
            'application/json' => 'yii\web\JsonParser',
        ],
    ],
    'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            ['class' => 'yii\rest\UrlRule', 'controller' => 'product'],
        ],
    ],
    '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'),
],
'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;

模型产品.php

    namespace app\models;

use yii\db\ActiveRecord;

/**
 * This is the model class for table "product".
 *
 * @property integer $ProductID
 * @property string $Name
 * @property double $Price
 * @property string $ShortDesc
 * @property string $LongDesc
 * @property string $PicUrl
 */
class Product extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'product';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Price'], 'number'],
            [['ShortDesc', 'LongDesc', 'PicUrl'], 'string'],
            [['Name'], 'string', 'max' => 60]
        ];
    }

    /**
     * @inheritdoc
     */
    public static function primaryKey()
    {
        return ['ProductID'];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ProductID' => 'Product ID',
            'Name' => 'Name',
            'Price' => 'Price',
            'ShortDesc' => 'Short Desc',
            'LongDesc' => 'Long Desc',
            'PicUrl' => 'Pic Url',
        ];
    }
}

控制器 ProductController.php

use yii\rest\ActiveController;

class ProductController extends ActiveController
{
    public $modelClass = 'app\models\Product';
}

我尝试关闭“enableStrictParsing”并将 $pluralize 设置为 false,但没有成功。

我也尝试添加这个 .htaccess 文件,它给了我 500 而不是 404。

RewriteEngine on

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

我确定我在这里做了一些愚蠢的事情,但任何愿意指出这一点的人都会有很大的帮助。

谢谢!

【问题讨论】:

  • 是否有可能在 Apache 中有需要打开的东西没有被 requirements.php 捕获?

标签: php .htaccess rest yii


【解决方案1】:

只需在urlManager 数组中将enableStrictParsing 设置为false。

【讨论】:

    【解决方案2】:

    扇贝船 - 我遇到了同样的问题。我在 Yii2 论坛上提出并得到了答案。您应该使用的网址是http://localhost/basic/web/index.php/products。

    还建议我将“showScriptName”更改为 true。建议是,否则链接将无法生成。

    还有人建议我可以编写一些重写规则来使用更简洁的 url。

    【讨论】:

      【解决方案3】:

      @scallopboat - 我也有类似的问题。我收到了 view 操作的 404 响应。

      按照来自的说明解决,

      How to configure urlManager to use rest api with string id

      'rules' => [
                  [
                      'class' => 'yii\rest\UrlRule', 
                      'controller' => 'region',
                      'pluralize'=>false,
                      'tokens' => [ '{id}' => '<id:\\w+>' ]
                  ],
              ],
      

      使用以下网址访问view 操作:

      @987654322@

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        扇贝船。当您还没有解决问题时,您可以尝试这种方式。 如果你使用 Apache 作为你的 web 服务器,你应该

        • 编辑 Apache 配置文件 httpd.conf 并找到类似的块 &lt;Directory&gt;****&lt;/Directory&gt;和<Directory "your web root dir">****</Directory>,修改AllowOverride None为 AllowOverride All
        • 取消注释#LoadModule rewrite_module modules/ApacheModuleRewrite.dll
        • 将.hataccess 文件放在 index.php 的基目录中

        【讨论】:

          猜你喜欢
          • 2015-02-24
          • 2022-10-24
          • 1970-01-01
          • 2012-02-08
          • 2014-10-11
          • 1970-01-01
          • 1970-01-01
          • 2016-08-08
          • 1970-01-01
          相关资源
          最近更新 更多