Not sure if this is the best way to do it but I create a Controller that extends ActiveController with all my default information and I extend that one with my “real” controllers.
Oh ok! Then you could update your urlManager or do what @echantigny said - create a custom controller, change its behavior and extend on the other controllers.
For defining allowed HTTP methods there is the VerbFilter behavior which can be attached to a controller and defines which actions can receive which HTTP methods. However it seems VerbFilter is not designed to be attached to the application directly as it is possible with AccessConrol filter for example.
To allow Verbfilter to work for multiple controllers you need to extend it:
<?php
namespace app\components;
class VerbFilter extends \yii\filters\VerbFilter
{
public $controllers = [];
/**
* @param ActionEvent $event
* @return bool
* @throws MethodNotAllowedHttpException when the request method is not allowed.
*/
public function beforeAction($event)
{
foreach($this->controllers as $controllerClass => $verbs) {
if ($event->action->controller instanceof $controllerClass) {
$this->actions = ['*' => $verbs];
return parent::beforeAction($event);
}
}
return $event->isValid;
}
}
You can configure the application like this:
return [
// ... application config ...
'as verbFilter' => [
'class' => \app\components\VerbFilter::class,
'controllers' => [
\app\controllers\SiteController::class => [
['GET', 'POST'], // only GET and POST allowed in web controllers
],
// you may also specify an interface and implement
// that in all RestControllers
\app\components\RestControllerInterface::class => [
['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], // all methods allowed in RestControllers
],
]
]
You may add additional VerbFilter in Controller classes which will limit HTTP methods for specific actions.