cakephp requestAction method View file
How 开发者_JAVA技巧can i call a requestAction
method from my view
, to specific controller
, which returns
me results on basis of conditions i mention?
Thanks !
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 pass requestAction
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:
$blogs = $this->requestAction('/blogs/index');
You can also invoke requestAction
by passing it an array of route components, in the same way as you might pass them into HtmlHelper::link
. So you might retrieve the collection of blogs thus:
$blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index');
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:
$blogs = $this->requestAction('/blogs/index/author_id:1234');
// or
$blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index', 'author_id' => 1234);
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:
class BlogsController extends AppController{
function index(){
$conditions = array();
if ( isset($this->params['author_id'])){
$conditions['Blog.author_id'] = $this->params['author_id'];
}
$blogs = $this->Blog->find('all', array('conditions'=>$conditions);
if ( isset($this->params['requested']) && $this->params['requested'] == 1){
return $blogs;
}
else{
$this->set(compact('blogs'));
}
}
}
The if
statement checking the presence and value of $this->params['requested']
is the key part. It checks whether the action was invoked by requestAction
or not. If it was, it returns the collection of blogs returned by Blog::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.
$comments = $this->requestAction('/comments/latest');
foreach($comments as $comment) {
echo $comment['Comment']['title'];
}
put this code in your ctp file
精彩评论