fallsleep
fallsleep

Reputation: 35

How Cake PHP pass parameters from one controller action to another view

I have a search method in my WorkersController.php like following:

public function search() {
    $conditions = array();
    if(!empty($this->request->data)){
        foreach($this->request->data['Search'] as $field => $search_condition ) {
            if(!empty($search_condition))
                $conditions["$field LIKE "] = "%$search_condition%";
        }
    }
    if(!empty($conditions)){
        $this->Worker->recursive = 0;
        $workers = $this->Worker->find('all',array('conditions' => $conditions));
    }
    $this->redirect(array('action' => 'index','search' ));
}

IN the method I call redirect(), then the page goes to index.ctp, where I want to fetch $workers like this:

if($this->request->params['pass']==array('search')){
    if (empty($workers)){
        echo('No result found!');
    }else{
        foreach ($workers as $worker){ 
            //do something
        }
    }
}

But I just can't fetch $workers, how can I pass it from search() to index.ctp?

Thanks a lot!

Upvotes: 2

Views: 4905

Answers (2)

Nick
Nick

Reputation: 189

You could try and use Session for this case.

//in controller1 $this->Session->write('worker', $workers);

//in controller2 $workersData = $this->Session->read('worker');

Upvotes: 3

liyakat
liyakat

Reputation: 11853

yes you can use as per below syntax to

$this->redirect(array('controller' => 'workers', 'action' => 'index', 'pass' => 'param', 'pass1' => 'param1'));

for more detail you can use doc

Upvotes: 0

Related Questions