sam
sam

Reputation: 956

How to Route Yii2 REST API module

am building Yii2 REST API for the first timw and following some documentation online I came up with the below, but after setup i got 404 error and I don't know where this error is coming from, maybe a problem with my REST URL or problem my my API module. Any help on this would be appreciated

Controller class
<?php

namespace api\modules\v1\controllers;

class CountryController extends \yii\rest\ActiveController
{
    public $modelClass = 'api\modules\v1\models\Country'; 
}



Model Class

<?php

namespace api\modules\v1\models;

use Yii;

/**
 * This is the model class for table "country".
 *
 * @property string $code
 * @property string $name
 * @property int $population
 */
class Country extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'country';
    }
    
    public static function primaryKey(){ 
        return ['code']; 
        
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['code', 'name'], 'required'],
            [['population'], 'integer'],
            [['code'], 'string', 'max' => 2],
            [['name'], 'string', 'max' => 52],
            [['code'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'code' => 'Code',
            'name' => 'Name',
            'population' => 'Population',
        ];
    }
}

Config 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'],
    'controllerNamespace' => 'api\modules\v1\controllers',
    'modules' => [      
        'v1' => [
            'basePath' => '@api/modules/v1',
            'class' => 'api\modules\v1\Module',
        ],

    ],
    'components' => [
        'request' => [
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ],
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => false,
        ],

        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],

       'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => ['v1/country'],
                    'pluralize' => false,
                    'tokens' => ['{id}' => 'id:\\\\w+'],
                ],                  
            ],
        ],
    ],
    'params' => $params,
];

When I try to send a get request to http://localhost/country from postman i got 404 i also tried http://localhost/v1/country but got 404 too, what i'm i doing wrong below is the directory sturcture of my api module

enter image description here

updated tying error in my question I visited http://localhost/country not http://localhost/countries below is a postman screenshot

enter image description here

enter image description here

Upvotes: 0

Views: 297

Answers (0)

Related Questions