I created one controller SlideController for manage the slide on my website.
But In the SiteController I just want to load some action in the SlideController.
Is it possible or not? If it's possible ,how can I?
2 Answers
Yes, it is possible.
The proper way I think, is to create yii2\base\Action subclasses which contain common actions shared by different controller.
As a reference on how to write Action and how controller links up it, you can read the source code here - https://github.com/yiisoft/yii2/tree/master/framework/rest
4 Comments
Chhorn Soro
Sorry , I'm really don't understand what it is. Could you give any example?
Victor Wong
Basically, there are two ways to define action in controller, one is declaring
actionXXX() method (where XXX is the name of the action), another one is returning an array of "action name-value pair" under actions() method. For the second way, examples are inside the link above. Read ActiveController and xxxAction classes.Chhorn Soro
I know how to create an action , but I don't know how can I access that action from others controller?
Victor Wong
I don't quite understand your problem. If you can make ControllerA to access ActionA, why can't you make ControllerB to access ActionA too?
You can use this for your need: https://stackoverflow.com/a/30432980/748156
In your case, in SiteController, set it like:
Yii::$app->runAction('slide/action', ['param1'=>'value1', 'param2'=>'value2']);