RestAPI Yii2

Hi all

now i’m starting my new application in yii2 with all the operations based Restful API.

My Folder structure is

myapp

|-- api

| |-- config

| |-- modules

| | `-- v1

| |-- controllers

| `-- models

         Modules.php 

| |-- runtime

| |-- web

|-- assets

| | `-- css

| `–

|-- assets

|-- commands

|-- config

|-- controllers

|-- mail

|-- models

|-- runtime

|-- tests

|-- vendor

|-- views

|-- web

/api/index.php


<?php


// comment out the following two lines when deployed to production

defined('YII_DEBUG') or define('YII_DEBUG', true);

defined('YII_ENV') or define('YII_ENV', 'dev');


require(__DIR__ . '/../vendor/autoload.php');

require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');


$config = require(__DIR__ . '/config/api.php');


(new yii\web\Application($config))->run();

/api/config/api.php


<?php


$db     = require(__DIR__ . '/../../config/db.php');

$params = require(__DIR__ . '/params.php');

 

$config = [

    'id' => 'basic',

    'name' => 'TimeTracker',

    // Need to get one level up:

    'basePath' => dirname(__DIR__).'/..',

    'bootstrap' => ['log'],

    'components' => [

        'request' => [

            // Enable JSON Input:

            'parsers' => [

                'application/json' => 'yii\web\JsonParser',

            ]

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                     // Create API log in the standard log dir

                     // But in file 'api.log':

                    'logFile' => '@app/runtime/logs/api.log',

                ],

            ],

        ],

        'urlManager' => [

            'enablePrettyUrl' => true,

            'enableStrictParsing' => true,

            'showScriptName' => false,

            'rules' => [

                ['class' => 'yii\rest\UrlRule', 'controller' => ['v1/project','v1/time']],

            ],

        ], 

        'db' => $db,

    ],

    'modules' => [

        'v1' => [

            'class' => 'app\api\modules\v1\Module',

        ],

    ],

    'params' => $params,

];

 return $config;

api/modules/v1/Module.php


<?php

namespace app\api\modules\v1;

 

class Module extends \yii\base\Module

{

    public function init()

    {

        parent::init();

 

        // ...  other initialization code ...

    }

}

api/modules/v1/models/Country.php


<?php

namespace app\api\v1\models;

use \yii\db\ActiveRecord;


class Country extends ActiveRecord 

{

        /**

        * @inheritdoc

        */

        public static function tableName()

        {

                return 'country';

        }


        /**

        * @inheritdoc

        */

        public static function primaryKey()

        {

        return ['country_id'];

        }


        /**

        * Define rules for validation

        */

        public function rules()

        {

        return [

                [['name', 'status'], 'required']

        ];

        }   

}



api/modules/v1/controllers/CountryController.php


<?php

namespace app\api\v1\controllers;

 

use yii\rest\ActiveController;


class CountryController extends ActiveController

{

        public $modelClass = 'app\api\v1\models\Country';

}




I try access using http://localhost/myapp/web/api/v1/country/

but 404 NOt found.

Anyone help me out would be grateful…

Thanks in Advance

1 Like

You have to add country in $config > urlmanager > rules > controller


 'controller' => ['v1/project','v1/time', 'v1/country']],

… and use plural in your url:


http://localhost/myapp/web/api/v1/countries/


http://localhost/myapp/web/api/v1/projects/

[color="#006400"]/* Moved from "General Discussions" to "REST APIs" */[/color]

Added but not working

Hi

Tried with this url

http://localhost/myapp/api/v1/projects

Works fine but PUT method is not working.I wil post the updated code

does your server accept PUT requests ? I had a similar issue once with IIS server. this solved it: http://stackoverflow.com/questions/6739124/iis-7-5-enable-put-and-delete-for-restful-service-extensionless

I have the same issue, I think the issue with pretty url, i’m using Nginx I could not fix this issue, and I found API with advanced much easier.