How can i call a requestAction
method from my view
, to specific controller
, which returns
me results on basis of conditions i mention?
Thanks !
How can i call a requestAction
method from my view
, to specific controller
, which returns
me results on basis of conditions i mention?
Thanks !
put this code in your ctp file
Generally speaking, using
requestAction
is not very performant, because it begins a whole new dispatch process -- in essence your application is handling two requests for every one request made by a user. For this reason, you want to avoid using it as much as possible. That being said,requestAction
has its uses, and you can mitigate the performance hit using view caching.It's hard to gauge precisely what you want to do with
requestAction
, but the fundamental concept is you're asking CakePHP to process another request, so you can simply passrequestAction
any URL your app would handle if it were typed into a browser's address bar (excluding the protocol and hostname). If you wanted to retrieve a collection of blogs managed by your application:You can also invoke
requestAction
by passing it an array of route components, in the same way as you might pass them intoHtmlHelper::link
. So you might retrieve the collection of blogs thus:Insofar as filtering the result-set returned by
requestAction
, it again is done by passing the conditions as part of the URL or route components:Note that if you want your requested action to return values, you need the action to handle requests differently than standard action requests. For the
BlogsController::index
action I've been referring to above, it might look something like this:The
if
statement checking the presence and value of$this->params['requested']
is the key part. It checks whether the action was invoked byrequestAction
or not. If it was, it returns the collection of blogs returned byBlog::find
; otherwise, it makes the collection available to the view, and allows the controller to continue onto rendering the view.There are many nuances to using
requestAction
to arrive at the specific results you require, but the above should provide you with the basics. Check out the links posted by dogmatic69 for additional documentation, and of course there are numerous stacko questions on the subject.Feel free to comment with any follow-ups! Hope this helped.