In case you need to execute a code before every controller and action, you can do like below:
1 - Add a component into your components directory, for example(MyGlobalClass
):
namespace appcomponents;
class MyGlobalClass extends yiiaseComponent{
public function init() {
echo "Hi";
parent::init();
}
}
2 - Add MyGlobalClass
component into your components array in config file:
'components' => [
'MyGlobalClass'=>[
'class'=>'appcomponentsMyGlobalClass'
],
//other components
3 - Add MyGlobalClass
into bootstarp
array in config file:
'bootstrap' => ['log','MyGlobalClass'],
Now, you can see Hi
before every action.
Please note that, if you do not need to use Events
and Behaviors
you can use yiiaseObject
instead of yiiaseComponent
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…