Yii 2.0 Restful Routing 404问题

问题描述 投票:2回答:4

我正在尝试使用我的简单产品表(而不是用户)遵循此处找到的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;

模型Product.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

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

谢谢!

php .htaccess rest yii
4个回答
2
投票

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


1
投票

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

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

还建议我可以编写一些重写规则以使用更干净的URL。


1
投票

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

按照以下说明解决,

如何配置urlManager以使用带有字符串ID的rest api

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

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

http://localhost/icdp-service/region/[mongo-id]

希望这可以帮助。


0
投票

Scallopboat。 当您还不能解决问题时,可以尝试这种方式。 如果您将Apache用作Web服务器,则应

  • 编辑Apache配置文件httpd.conf并找到<Directory>****</Directory><Directory "your web root dir">****</Directory> ,将AllowOverride None修改为AllowOverride All
  • 取消注释#LoadModule rewrite_module modules/ApacheModuleRewrite.dll
  • .hataccess文件放置在index.php的基本目录中
© www.soinside.com 2019 - 2024. All rights reserved.