Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Controller action in another controller in cakephp?

I want to use "getpoll" ,which is action of users controller ,in another controller(events controller).

How can i use it?

in advance thanks...to all...

like image 485
chetanspeed511987 Avatar asked Jul 07 '11 05:07

chetanspeed511987


3 Answers

You can use requestAction method of the controller:

$this->requestAction('/comments/latest');

you can call it differently depending on your needs for details look for the link:

Link to CookBook: Controller requestAction Method

like image 104
Headshota Avatar answered Nov 20 '22 06:11

Headshota


You should write the db query of the getPoll() action as a method in the poll model, that way in your user controller you can just call $this->User->Poll->getPolls() to grab the polls, and if associations are setup correctly, $this->Event->Poll->getPolls() from your events controller.

For example in your poll model:

public function getPoll($userId = null) {
    return $this->find('all', array(..));
}
like image 3
Dunhamzzz Avatar answered Nov 20 '22 07:11

Dunhamzzz


You could share a common piece of code between controllers with components.

http://book.cakephp.org/view/994/Introduction

like image 2
Henri Avatar answered Nov 20 '22 07:11

Henri