Is it possible to call another controlerr’s action from actual controller?
[indent]
example:
class actualControler extends CController {
public function actionOne() {
$fromAnother = <here I want to call anoterAction>;
$this->render('one',array('fromAnother'=>$fromAnother));
}
}
class anotherController extends CController {
public function anotherAction() {
// do something and return it
}
}
[/indent]
I want to call anoterController anotherAction from actualController actionOne
You could import the whole controllers folder in config if needed.
But I guess you’ll be better when you put that method into a model or a special component since the controller-action you want to call obviously doesn’t function as a real controller-action.
class ServiceController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.back.ConsolidateController'); // ConsolidateController is another controller in back controller folder
echo ConsolidateController::test(); // test is action in ConsolidateController
class ServiceController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.back.CservicesController');
$obj =new CservicesController(); // preparing object
echo $obj->test(); exit; // calling test class of ServiceController
If you are going to utilize an action of another controller, the best thing is CController::forward().
It is a wrapper around Yii::app()->runController.
Or you can directly call like this.
Yii::app()->runController('catagory/index'); //catagory is the controller id;index is action id.
Yii::app()->runController('catagory/view/id/1');
If you want to use methods of of another controller, we can do get the instance like this.
$cat=Yii::app()->createController('catagory');//returns array containing controller instance and action index.
$cat=$cat[0]; //get the controller instance.
$cat->loadModel(1); //use a public method.
Or using the action() method inside Controllers to map certain action to certan file, if it is for actions accesibles from the web. But that way the controller will have the same action name, for example, action is named ‘actionDeletePost’ you can use it in AdminController and in UserController, appending /deletePost to the URL.
class Controller A extends Controller
{
public function action save()
{
$array1 = $_POST['array'];
$A = $this->redirect(array('controller B/insert','data'=>$array1));
echo $a;
}
}
In My controller B
class Controller B extends Controller
{
public function action Insert($data)
{
echo $data;
/*----code-----*/
return value;
}
}
I am newbie in yii framework. I have problem with, call controller b action Insert() from controller a with parameters. But i have error like "400 Your request is invalid.". i don't know how to fix it. please help me anyone..! Sorry for my English.. Thank You..
{
if (Yii::$app->user->isGuest) {
return Yii::$app->runAction('site/login'); //calling login action in site Controller for showing login form to the user
}
}