5

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

chetanspeed511987
  • 1,995
  • 2
  • 22
  • 34
  • Does this action have a view? – 8vius Jul 26 '11 at 03:09
  • possible duplicate of [CakePHP 2.3.8: Calling Another Controller function in CronController.php](http://stackoverflow.com/questions/19344988/cakephp-2-3-8-calling-another-controller-function-in-croncontroller-php) – Pipo Sep 29 '15 at 10:46

4 Answers4

4

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

Headshota
  • 21,021
  • 11
  • 61
  • 82
  • 1
    You should not use the requestAction for something as simple as this, the proper advice would be to put the getPoll method in the user *model*. – Dunhamzzz Jul 07 '11 at 08:12
3

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(..));
}
Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
2

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

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

Henri
  • 740
  • 10
  • 22
0

The cookbook states:

If used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model. http://book.cakephp.org/2.0/en/controllers.html

And this post show a different and better approach

Community
  • 1
  • 1
Mike
  • 81
  • 2