Yii2 restful api 找不到对象错误
Yii2 restful api object not found error
我试图从这个 http://budiirawan.com/setup-restful-api-yii2/ 安装 RESTful API 模块,但出现错误
Object not found!
我尝试设置 mod_rewrite
和 AllowOverride All
配置。
我也已将它连接到正确的数据库,该数据库中有 country
table。
我也有 .htaccess
文件,这是我的 api/config/main.php
文件
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'modules' => [
'v1' => [
'basePath' => '@app/modules/v1',
'class' => 'api\modules\v1\Module'
]
],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'tokens' => [
'{id}' => '<id:\w+>'
]
]
],
]
],
'params' => $params,
];
这是模型Country
<?php
namespace api\modules\v1\models;
use yii\db\ActiveRecord;
/**
* Country Model
*
* @author Budi Irawan <deerawan@gmail.com>
*/
class Country extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'country';
}
/**
* We use the primary function because we don't use integer auto increment as a primary key.
* @inheritdoc
*/
public static function primaryKey()
{
return ['code'];
}
/**
* To let Yii know what fields exist on the table.
* Define rules for validation
*/
public function rules()
{
return [
[['code', 'name', 'population'], 'required']
];
}
}
通过http://localhost/yii2-api/api/v1/countries访问它时,我仍然遇到同样的错误。
根据教程你必须使用 url :
http://localhost/yii2-api/api/web/v1/countries
而不是
http://localhost/yii2-api/api/v1/countries
就我而言,
mysql table 有复合主键(错误创建),
并且只有 GET /v1/othertable/1 因此错误而失败,而其他路由正在运行。
当我通过 PhpMyAdmin 对索引进行排序时,它开始工作了。
我试图从这个 http://budiirawan.com/setup-restful-api-yii2/ 安装 RESTful API 模块,但出现错误
Object not found!
我尝试设置 mod_rewrite
和 AllowOverride All
配置。
我也已将它连接到正确的数据库,该数据库中有 country
table。
我也有 .htaccess
文件,这是我的 api/config/main.php
文件
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'modules' => [
'v1' => [
'basePath' => '@app/modules/v1',
'class' => 'api\modules\v1\Module'
]
],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'tokens' => [
'{id}' => '<id:\w+>'
]
]
],
]
],
'params' => $params,
];
这是模型Country
<?php
namespace api\modules\v1\models;
use yii\db\ActiveRecord;
/**
* Country Model
*
* @author Budi Irawan <deerawan@gmail.com>
*/
class Country extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'country';
}
/**
* We use the primary function because we don't use integer auto increment as a primary key.
* @inheritdoc
*/
public static function primaryKey()
{
return ['code'];
}
/**
* To let Yii know what fields exist on the table.
* Define rules for validation
*/
public function rules()
{
return [
[['code', 'name', 'population'], 'required']
];
}
}
通过http://localhost/yii2-api/api/v1/countries访问它时,我仍然遇到同样的错误。
根据教程你必须使用 url :
http://localhost/yii2-api/api/web/v1/countries
而不是
http://localhost/yii2-api/api/v1/countries
就我而言, mysql table 有复合主键(错误创建), 并且只有 GET /v1/othertable/1 因此错误而失败,而其他路由正在运行。 当我通过 PhpMyAdmin 对索引进行排序时,它开始工作了。