0

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?

Victor Wong
  • 2,486
  • 23
  • 32
Chhorn Soro
  • 3,061
  • 8
  • 27
  • 43

2 Answers2

1

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

Victor Wong
  • 2,486
  • 23
  • 32
  • Sorry , I'm really don't understand what it is. Could you give any example? – Chhorn Soro Jan 16 '15 at 02:28
  • 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. – Victor Wong Jan 16 '15 at 03:40
  • I know how to create an action , but I don't know how can I access that action from others controller? – Chhorn Soro Jan 16 '15 at 04:03
  • 1
    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? – Victor Wong Jan 16 '15 at 06:38
0

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']);
Mahesh
  • 603
  • 1
  • 10
  • 22