Add params to layout in every controller

Hi everybody,

I need to add params to the layout in every controller (or in a list of controller I can decide).

I use vendor components that have their controller, and I would like to not edit those controllers.

How can I do that?

With a filter? A component? Anything else?

Thanks in advance.

My suggestion is simply store it in session, access the session value in your view/template.

You can add the following as first line (after namespace and use declaration) in your template:




$myParams=Yii::$app->session->get('myParams');



then you can use $myParams directly in you view/template at any line

I was thinking about a component, like this stackoverflow answer:

http://stackoverflow.com/questions/27180059/execute-my-code-before-any-action-of-any-controller

In the component I would get the current controller and I’d do something like this:




$controller->view->params['name']



what you think?

Yes it can be another solution but if you need just a value (or an array of values) to be avail in view/template or other parts of code it seem to me that this add a bit of overhead.

In my opinion the stackoverflow method is good when you need to do some more complex operation and you need it to be executed without adding code here and there.

Consider also that if the parameter is not constant and change during user operation, in the end you will store/retrive it in session through the class.

For passing value, I would still go directly for session storage, I’m for the KISS approach.

Thanks for your reply.