php - Yii 2.0 静态路由 404 问题

标签 php .htaccess rest yii

我正在尝试使用我的简单产品表来遵循此处找到的 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',
        ];
    }
}

Controller 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

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

谢谢!

最佳答案

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

关于php - Yii 2.0 静态路由 404 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27763399/

相关文章:

php - 仅第三个变量中的非法字符串偏移

Php5-客户端未运行

Apache : Restrict access to URI, 不是资源

php - 为什么身份验证 token 被认为是无状态的,而 session 则不是?

php - 使用 PHP 创建带有任务计划的日历

javascript - 从 Javascript/Geocoder.geocode() 获取纬度和经度并将其与 php 一起使用

rest - 使用Atom进行资源收集时的REST API版本控制

wcf - WebGet UriTemplate 在点 ('.'/%2E 上失败)?

apache - htaccess 递归 mod_rewrite

php - 防止直接 url 访问文件