REST API advanced template 404ing


(Robertnolan) #1

I have a web app created using the advanced template. I am now trying to implement a REST API. When I make a request to the API (http://dev.site/api/v1/gyms) I get a 404

My project layout is as follows:


api

|config

|->main.php

|->params.php

|modules

||v1

|||controllers

|||->GymController.php

|||models

|||->Gym.php

||->Module.php

|runtime

|web

backend

common

console

environments

frontend

vendor

And here is my apache conf:


<VirtualHost *:80>

    ServerName dev.site

    #ErrorLog /dev/null

    #LogLevel emerg

    #CustomLog /dev/null combined


    RewriteEngine on

    RewriteCond %{REQUEST_URI} !^/(backend/web|admin|api/web|api)

    RewriteRule !^/frontend/web /frontend/web%{REQUEST_URI} [L]


    RewriteCond %{REQUEST_URI} ^/admin$

    RewriteRule ^/admin /backend/web/index.php [L]

    RewriteCond %{REQUEST_URI} ^/admin

    RewriteRule ^/admin(.*) /backend/web$1 [L]


    RewriteCond %{REQUEST_URI} ^/api$

    RewriteRule ^/api /api/web/index.php [L]

    RewriteCond %{REQUEST_URI} ^/api

    RewriteRule ^/api(.*) /api/web$1 [L]


    DocumentRoot /path/to/project

    <Directory />

        Options FollowSymLinks

        AllowOverride None

        AddDefaultCharset utf-8

    </Directory>

    <Directory /path/to/project/frontend/web>

        RewriteEngine on

        # if a directory or a file exists, use the request directly

        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        # otherwise forward the request to index.php

        RewriteRule . index.php


        # Apache 2.4

        Require all granted


        ## Apache 2.2

        #Order Allow,Deny

        #Allow from all

    </Directory>

    <Directory /path/to/project/backend/web>

        RewriteEngine on

        # if a directory or a file exists, use the request directly

        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        # otherwise forward the request to index.php

        RewriteRule . index.php


        # Apache 2.4

        Require all granted


        ## Apache 2.2

        #Order Allow,Deny

        #Allow from all

    </Directory>

    

    <Directory /path/to/project/api/web>

        RewriteEngine on

        # if a directory or a file exists, use the request directly

        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        # otherwise forward the request to index.php

        #RewriteRule . index.php


        # Apache 2.4

        Require all granted


        ## Apache 2.2

        #Order Allow,Deny

        #Allow from all

    </Directory>


    <FilesMatch \.(htaccess|htpasswd|svn|git)>

        Deny from all

        Satisfy All

    </FilesMatch>

</VirtualHost>

GymController.php looks like this:




namespace api\modules\v1\controllers;


use yii\rest\ActiveController;


class GymController extends ActiveController

{

    public $modelClass = 'api\modules\v1\models\Gym';

}

Module.php looks like this:


namespace api\modules\v1;

class Module extends \yii\base\Module

{

    public $controllerNamespace = 'api\modules\v1\controllers';

    public function init()

    {

        parent::init();

    }

}

and main.php looks like this:


$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' => '@api/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/gym',

                    'tokens' => [

                        '{id}' => '<id:\\w+>'

                    ]

                    

                ]

            ],        

        ]

    ],

    'params' => $params,

];

I feel like I’m missing something really simple here but I just can’t seem to figure it out, anyone got any ideas?


(Peter Verbrugge) #2

Did you figure it out yet? I am having the same issue here, and it’s starting to get really frustrating. No tutorial online is actually working.