An old cave filled with thoughts on old school fantasy RPGS and their simulacrum like Dungeons and Dragons and Labyrinth Lord.
Cake's requestAction() Method
20 May 2007 03:58:30
Category: CakePHP
The Cake requestAction method allows you to call another action from an
action. This function is handy because you don't have to actually
redirect a user to get the output or results of another action in your
code. The purpose of this post is to try and fill in the documentation
gap for this important feature of Cake.
Lets say a user has requested a page in your Cake application something like 'blogs/view'. But we want to record the visit it in a log as well as write the request to session so we can track viewed pages. We've already written code to do this in the log controller of our application. So we want to request this action. The code would be something like this:
What happens?
When requestAction() is invoked, we start a new execution thread at the top of the dispatcher. All of the calculations that take place there are again reworked. The log controller is instantiated and the "visit" method is invoked. This action is now "nested" inside of the parent action call and output will be affected accordingly.
There are a few features of this method that are not documented well that I will point out here. This action takes a second parameter that's documented as "extra." But the array doesn't work like other arrays passed about in cake. There are a few named 'controls' or keys that you can pass in here. Use 'pass' to pass variables and use 'bare' to suspend output. So if you wanted to pass three arguments and suspend any output of the action, your request would look like this:
This is an important feature of Cake that is not used often but when you need it, it comes in handy.
Lets say a user has requested a page in your Cake application something like 'blogs/view'. But we want to record the visit it in a log as well as write the request to session so we can track viewed pages. We've already written code to do this in the log controller of our application. So we want to request this action. The code would be something like this:
$this->requestAction('log/visit');
What happens?
When requestAction() is invoked, we start a new execution thread at the top of the dispatcher. All of the calculations that take place there are again reworked. The log controller is instantiated and the "visit" method is invoked. This action is now "nested" inside of the parent action call and output will be affected accordingly.
There are a few features of this method that are not documented well that I will point out here. This action takes a second parameter that's documented as "extra." But the array doesn't work like other arrays passed about in cake. There are a few named 'controls' or keys that you can pass in here. Use 'pass' to pass variables and use 'bare' to suspend output. So if you wanted to pass three arguments and suspend any output of the action, your request would look like this:
$this->requestAction('log/visit',
array('pass'=>array('arg1', 'arg2', 'arg3'), 'bare'=>
true);
This is an important feature of Cake that is not used often but when you need it, it comes in handy.








