Mike Ross Mike Ross - 6 months ago 103
PHP Question

Yii2 restful api object not found error

I know that this might seem like a duplicate question but its not and i have been trying from last 3 days but not able to solve the problem so i am posting here.

I tried to install restful api module from this http://budiirawan.com/setup-restful-api-yii2/ and i am getting error


Object not found!


I tried setting
modrewrite
and also
AllowOverride All
configuration.

I also have connected it to correct database and that database has
country
table in it.

I also have
.htaccess
file and here is my
api/config/main.php
file

<?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,
];


Here is the model
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']
];
}
}


I still get the same error while accessing it through http://localhost/yii2-api/api/v1/countries

Let me know if anyone can help me solve this problem

Thank you

Answer

According to tutorial you have to use url :

http://localhost/yii2-api/api/web/v1/countries

Instead of

http://localhost/yii2-api/api/v1/countries
Comments