Rest With Modules (Versioning)

Hello.

Can’t figure out, what am I doing wrong. I want to make rest API with versioning (using advanced template). Here is my directory hierarchy:


api

  config

    main.php

    ...

  modules

    v1

      controllers

        TestController.php

  runtime

    ...

  web

    index.php

    ...

backend

  ...

common

  ...

frontend

  ...

That’s what I have in config (main.php):




<?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__),

    'controllerNamespace' => 'api\controllers', // I also tried to set 'api\modules\v1\controllers' here

    'bootstrap' => ['log'],

    'modules' => [

        'v1' => [

            'basePath' => '@api/modules/v1',

        ],

    ],

    'components' => [

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

        ],

        'user' => [

            'identityClass' => 'common\models\User',

        ],

        ...

    ],

    'params' => $params,

];



And this is controller code:




<?php

namespace api\modules\v1\controllers;


use yii\rest\Controller;


class TestController extends Controller

{

    public function actionIndex()

    {

        return 'ok';

    }

}



When I’m trying to get http://host/v1/test/index I’m getting #404. Before I decide to use modules for versioning everything was fine: http://host/test/index was showing results of action (controller was in api/controllers/TestController.php).

Please, help me to find my mistake.

I’ve found a solution - setting controllerNamespace inside module config:


<?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__),

    'controllerNamespace' => 'api\controllers',

    'bootstrap' => ['log'],

    'modules' => [

        'v1' => [

            'basePath' => '@api/modules/v1',

            'controllerNamespace' => 'api\modules\v1\controllers', // <-- here

        ],

    ],

    'components' => [

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

        ],

        'user' => [

            'identityClass' => 'common\models\User',

        ],

        ...

    ],

    'params' => $params,

];

Is it the right way? Why it is not documented in http://www.yiiframework.com/doc-2.0/guide-rest.html ?

By the way, how can I make Yii to not show full information about exceptions in rest response?

I don’t want to show internal errors to API consumers (especially SQL errors with query text). If I set YII_DEBUG to false, I still see type, name, message and code of exception and previous exception.

You can override ErrorHandler::convertExceptionToArray to give only the information you want to output.

That was already fixed by Samdark (I don’t remember the issue number).

I’ve solved my initial problem by creating dummy module class. See https://github.com/yiisoft/yii2/issues/4198

Thanks, CeBe.

Well I do not see your method in config since its not in list of default methods i.e create view update delete