P
P
Pios882018-07-16 19:29:02
Yii
Pios88, 2018-07-16 19:29:02

How to correctly set up routing for a nested module in Yii2?

I'm making a versioned REST ful API. The structure is:
Modules:
- api:
--- controllers
--------- BookController
--- models
--- Module.php
--- modules:
--------- controllers
--- ----------- BookController
--------- models
--------- Module.php
BookController:

class BookController extends ActiveController {
    public $modelClass = 'app\modules\api\models\Book';
}

'modules' => [
           'api' => [ 'class' => 'app\modules\api\Module', ],
           'v1' => [ 'class' => 'app\modules\api\modules\v1\Module', ],        
        ],
       'components' => [
          'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser' ],
          'baseUrl' => ''

'urlManager' => [
            'rules' => [
                '' => 'site/index',
                ['class' => 'yii\rest\UrlRule', 'controller' => 'api/book'],
                ['class' => 'yii\rest\UrlRule', 'controller' => 'api/v1/book'],
                 ],

When I request GET /api/books or GET /api/v1/books, I get the complete set of data about all the books.
When you try to complicate the functionality, for example, create several different collections, it naturally becomes necessary to add the appropriate actions to the BookController . But when I try to access, for example, a freshly created action, GET /api/books/list or GET /api/v1/books/list , I get a 404 status and Page not found in the response body. And when requesting GET /api/books?list or GET /api/v1/books?list(tried for the sake of experiment) I just get the action ignored and the full set of information about the books. And it doesn't matter what the name and content of the action I'm referring to.
When I turned on my brain, I realized that, apparently, the problem is banal in routing (I hope). Since I am in this business - the oak tree is strong, tell me how to write the routing rules correctly so that everything works correctly for both base controllers / actions, and module controllers / actions, and nested module controllers / actions?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question