How to call Controller action in another controller in cakephp?
Asked Answered
M

4

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

Multinuclear answered 7/7, 2011 at 5:32 Comment(2)
Does this action have a view?Semilunar
possible duplicate of CakePHP 2.3.8: Calling Another Controller function in CronController.phpGrubstake
E
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(..));
}
Elephus answered 7/7, 2011 at 8:14 Comment(0)
L
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

Laband answered 7/7, 2011 at 5:35 Comment(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.Elephus
E
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(..));
}
Elephus answered 7/7, 2011 at 8:14 Comment(0)
D
2

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

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

Diminution answered 7/7, 2011 at 6:3 Comment(0)
R
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

Rockie answered 28/7, 2013 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.