H
H
hobu4ok912016-09-22 05:27:02
Yii
hobu4ok91, 2016-09-22 05:27:02

How to set behavior for controllers in a module in Yii2?

I have created a rest api module and I need to assign behaviors to all controllers in this module. The behavior is:

$behaviors['contentNegotiator'] = [
    'class' => ContentNegotiator::className(),
    'formats' => [
        'application/json' => Response::FORMAT_JSON,
    ],
];

Naturally, I put it all into the behaviors method, first returned the parent behavior and added a new one.
If I add it to the controller, then everything works fine and the data is returned in json format, if I add it to the module, then nothing works. So the question is, is it really possible to set behaviors for controllers in a module, or do you need to create a parent controller, inherit it from the rest controller, define behavior in it, and then inherit your controllers from this controller?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2016-09-22
@slo_nik

Good morning.
Try adding the behavior file to the bootstrap section in the config file

'bootstrap' => [
'app\modules\name_module\YoumoduleBootstrap'
]

FILE YoumoduleBootstrap implements BootstrapInterface

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question