[SOLVED] Syntax for multiple class on beforeRequest

I managed to follow a tutorial to create language switcher. It requires the config file to do this :


   

    'controllerNamespace' => 'frontend\controllers',

   

    'as beforeRequest' =>  [

        'class' => 'common\components\LanguageHandler',

        

    ],    


    'params' => $params,

];




Then I duplicate all the code to create a theme switcher and did this :


   

    'controllerNamespace' => 'frontend\controllers',

   

    'as beforeRequest' =>  [

        'class' => 'common\components\LanguageHandler',

        'class' => 'common\components\ThemeHandler',

    ],    


    'params' => $params,

];




The language switcher does not anymore.

What is the correct way to write this? (injecting multiple classes to "as beforeRequest")?

Thanks

Can anyone help?

I tried these but still can’t get it to work. Sorry I am still unfamiliar with events.

I need to attach 2 event handler to the app.


   

    'controllerNamespace' => 'frontend\controllers',

   

    'as beforeRequest' =>  [

        'class' => array('common\components\LanguageHandler','common\components\ThemeHandler'),

    ],    


    'params' => $params,

];





   

    'controllerNamespace' => 'frontend\controllers',

   

    'as beforeRequest' =>  [

        'class' => ['common\components\LanguageHandler','common\components\ThemeHandler'],

    ],    


    'params' => $params,

];




Seems like you are trying to attach behaviors (i.e. subclasses of yii\base\Behavior) rather than event handlers (they are not the same). Just give them unique names:




'as languageHandler' => [

  'class' => 'common\components\LanguageHandler',

],

'as themeHandler' => [

  'class' => 'common\components\ThemeHandler',

],



Thank you phtamas! it works now.

Trying hard to understand the behaviour and event concept…

How do I edit the subject title to add [SOLVED]? can’t find it anywhere.

In a nutshell:

An event handler can be any PHP callable.

A behavior is a subclass of yii\base\Behavior. It can attach handlers to the events of its owner but it has additional features too.