Hi, we’re rewriting our REST API with Yii2 and I’d like to hear your thoughts about the ways to implement the nested REST API.

For example, I would like to support paths like GET /teams/1/players, DELETE /teams/1/players/1 , PATCH..., GET /teams/1/players/1 etc’.
I’m implementing the API with nested modules(/doc/guide/2.0/en/structure-modules#nested-modules).
Ways to implement the REST API I saw so far-

  1. The “normal” way of doing it is starting to load the app config with URL rules
  2. Implement the BootstrapInterface in modules and add to bootstrap to file.
  3. Use the magic of GitHub - tunecino/yii2-nested-rest: Nested routing support to the Yii RESTful API framework, which is being currently updated here - GitHub - MyCademy/yii2-nested-rest: Nested routing support to the Yii RESTful API framework. - I didn’t like that imo the package adds a bit too much dev overhead for implementing the REST.
  4. Write some runAction override
public function runAction($id, $params = [])
        $isIndex = empty($id) || $id == 'index';
        if (Yii::$app->request->isDelete && $isIndex) {
            $id = 'delete';
        } elseif (Yii::$app->request->isPatch && $isIndex) {
            $id = 'update';
        } elseif (Yii::$app->request->isPost && $isIndex) {
            $id = 'create';

        return parent::runAction($id, $params);
    }//end runAction()

The issue with this type of solution is that runAction runs if the URL is detected, maybe there’s a way to implement some catch all method and then parse it from there into modules/controllers?

I want to keep it simpler for future developers who might not be familiar with Yii2, is there a way to add Teams module/PlayersController add function actionIndex($first_id){} which will automagically detect the numbers in the URL as id’s and the names as modules/controllers/actions? I think that will probably work best or maybe it’s not such a good idea. Thoughts?


I usually do it “normal” way:

        'class' => 'yii\rest\UrlRule',
        'controller' => 'account',
        'extraPatterns' => [
            'GET user' => 'user',
            'PATCH user' => 'user-update',
            'orders' => 'orders',
            'addresses' => 'addresses',
        'pluralize' => false,

    'POST account/register' => '/user/rest/register-rest/register',
    'OPTIONS account/register' => '/user/rest/register-rest/register',
    'POST account/register-anonymous' => '/user/rest/register-rest/register-anonymous',