2015-01-04 47 views
2

我试图按照我的简单产品表 而不是用户在这里找到的Yii2.0示例。Yii 2.0宁静的路由404问题

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

我使用的基本包

,不先进。

当我尝试访问本地主机/产品 时,出现404错误。

当我使用localhost/index.php文件或本地主机/ 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', 
     ]; 
    } 
} 

use yii\rest\ActiveController; 

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

我曾试图关闭 'enableStrictParsing' 控制器ProductController.php并将$ pluralize设置为false,但没有运气。

我也尝试添加这给了我500,而不是404

RewriteEngine on 

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

我敢肯定,我已经做了一些在这里傻,但任何人都愿意指出这一点将这个.htaccess文件是一个巨大的帮助。

谢谢!

+0

是否有可能在Apache中有某些东西需要打开,这并未被requirements.php捕获? – scallopboat

回答

1

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

我还被建议将'showScriptName'更改为true。建议是,否则链接不会产生好的。

也有人建议我可以写一些重写规则来使用更干净的网址。

+0

太棒了!谢谢! – scallopboat

2

刚设置enableStrictParsingfalseurlManager数组。

+0

我试过了,它仍然不起作用。 – scallopboat

0

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

  • 编辑Apache的配置文件httpd.conf,并找出诸如 <Directory>****</Directory><Directory "your web root dir">****</Directory>块,修改AllowOverride NoneAllowOverride All
  • 取消对#LoadModule rewrite_module modules/ApacheModuleRewrite.dll
  • 地方.hataccess文件位于您index.php的基本目录中