Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        ));
    }
}
like image 301
user3318138 Avatar asked Feb 14 '23 06:02

user3318138


2 Answers

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.

like image 164
Samuel Liew Avatar answered Feb 15 '23 19:02

Samuel Liew


$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..

like image 32
Gaurav Porwal Avatar answered Feb 15 '23 20:02

Gaurav Porwal