2

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 mod_rewrite 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 <[email protected]>
 */
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.

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

In my case, the mysql table had compound primary key (made by mistake), and only GET /v1/othertable/1 failed with this error, while other routes are working. When I sorted that indexes through PhpMyAdmin it started to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.