The most reliable way of specifying new REST routes for custom actions in yii\web\UrlManager configuration

284 Views Asked by At

I have created a standard yii\rest\ActiveController and added my custom action to it:

public function actionTest($id)
{
    return ['test' => $id];
}

To make it work, I have added a new entry to extraPatterns of rules of yii\web\UrlManager:

'extraPatterns' => [
    'GET test/{id}' => 'test',
],

Tested, all OK. Then I wanted to support POST verb for this route, so I added another entry:

'rules' => [
    [
        'class' => 'yii\rest\UrlRule',
        'controller' => 'user',
        'pluralize' => false,
        'except' => ['index'],
        'extraPatterns' => [
            'GET test/{id}' => 'test',
            'POST test/{id}' => 'test',
        ],
    ],

Then DELETE and so on. And... there must be something that I am doing really wrong, because for:

  • Six methods: POST, GET, PUT, PATCH, OPTIONS and DELETE per each custom action
  • Five different actions per each controller and
  • Seven REST controllers

I'll end up with my main configuration file growing to... 6 * 5 * 7 = 210 lines?

So, what is in Yii 2 the best / the most reliable way of making sure that every custom action in every REST controller will be accessible for every method / verb and not making this by growing configuration file by hundreds of lines?

0

There are 0 best solutions below