php - The best way to include all controllers in yii2 rest urlmanager -


i using yii2 rest service. want include controllers in rest url manager. have config main.php below:

    'urlmanager' => [         'enableprettyurl' => true,         'enablestrictparsing' => false,         'showscriptname' => false,         'rules' => [             ['class' => 'yii\rest\urlrule', 'controller' => 'user'],             ['class' => 'yii\rest\urlrule', 'controller' => 'usera'],             ['class' => 'yii\rest\urlrule', 'controller' => 'userb'],             ['class' => 'yii\rest\urlrule', 'controller' => 'userc'],             ['class' => 'yii\rest\urlrule', 'controller' => 'userd'],         ],     ], 

can use this, not have include every controller in config file?

    'urlmanager' => [         'enableprettyurl' => true,         'enablestrictparsing' => false,         'showscriptname' => false,         'rules' => [             ['class' => 'yii\rest\urlrule', 'controller' => '*'],         ],     ], 

you may use 2 ways: 1) use array in controller property

'rules' => [             ['class' => 'yii\rest\urlrule', 'controller' => ['user', 'usera', 'userb'....]],          ], 

but must set controllers.

2) create new class , extends yii\rest\urlrule.

namespace app\rest; class urlrulecustom extends urlrule {   $path = '@app/controllers';   public function init()   {     $d = dir(yii::getalias($this->path));     $arr = [];     while (false !== ($entry = $d->read())) {        if (strpos($entry, 'controller.php') !== false) {           $arr[] = lcfirst(str_replace(['controller.php'], '', $entry));        }     }      $this->controller = $arr;            parent::init();   } } 

and in urlmanager:

'rules' => [             ['class' => 'app\rest\urlrulecustom', 'path' => '@app/controllers'],         ], 

but don't know did work yii::getalias in urlrulecustom. if didn't work set instead of absolute path or relative.


Popular posts from this blog