user3318138
user3318138

Reputation: 119

Call another action from one action of the same controller

I am new to YII. How to call an action from another action of the same controller. Suppose I am in action A. I need to call action B with two parameters. My controller name is Master Controller. How will I perform this. Please say me a solution. Thanks in advance.

My controller:

class NimsoftController extends Controller
{
    public function actionStore_action($action,$data_id)
    {
          $model= new NimsoftUserLoginAction;
          $model->user_id=Yii::app()->user->id;
          $model->action=$action;//"Deleted";
          $model->affected_data_id=$data_id;//"22";
          $model->save();
    }

    public function actionSearch($id)
    {
        $cust_id = $id;
        $criteria = new CDbCriteria();
        $criteria->condition = "host_customer_id = '$cust_id'";
        $details = NimsoftHost::model()->find($criteria);

        $criteria2 = new CDbCriteria();
        $criteria2->condition = "cust_id= '$details->host_customer_id'";
        $name = MasterCustomers::model()->find($criteria2);

        $dataProvider = new CActiveDataProvider('NimsoftHost', array(
            'criteria' => $criteria,
            'pagination'=>array('pageSize'=>40)
        ));
        $model = new NimsoftHost();
        $this->actionStore_action(array('action'=>'search', 'data_id'=>$cust_id));
        $this->render('index',array(
            'dataProvider' => $dataProvider,
            'details' => $details,
            'name' => $name->cust_name,
            'model' => $model
        ));
    }
}

Upvotes: 5

Views: 24203

Answers (4)

rajat saini
rajat saini

Reputation: 1

In case of forward your URL will not be change and you cannot send extra parameter in this.

$this->redirect('contact');

Upvotes: 0

Vikash Gupta
Vikash Gupta

Reputation: 121

You can do it this way also:-

Now you want to call action Name1 inside action Name2.

public function actionName1($param1,$param2) {

    //your code...

}

public function actionName2() {

    $this->redirect(array('name1','param1'=>'param1','param2'=>'param2'));

}

Upvotes: 1

Samuel Liew
Samuel Liew

Reputation: 79113

You can simply call the function directly, or,

$this->actionMyCustomAction(); exit;

you can perform a redirect to the action:

$this->redirect('anotherControllerName/myCustomAction');

or this if you are in the same controller:

$this->redirect('myCustomAction');

UPDATE:

You are passing the variables wrongly:

$this->actionStore_action(array('action'=>'search', 'data_id'=>$cust_id));

It should be two separate variables:

$this->actionStore_action('search', $cust_id);

This has NOTHING to do with Yii. It's only basic function calling.

Upvotes: 8

Gaurav Porwal
Gaurav Porwal

Reputation: 513

$this->forward('site/contact');

In case of forward your URL will not be change and you cannot send extra parameter in this.

If you want to send extra parameter then use redirect.

   $this->redirect(Yii::app()->createUrl('site/contact', array('id' => '22')));

Hope this will help you..

Upvotes: 1

Related Questions