Adi*_*att 1 controller cakephp model view
我如何可以调用requestAction从我的方法view,具体的controller,其中returns我对结果的条件我提到的基础?
谢谢 !
一般来说,使用requestAction不是非常高效,因为它开始了一个全新的调度过程 - 实质上,您的应用程序正在为用户发出的每个请求处理两个请求.因此,您希望尽可能避免使用它.话虽如此,requestAction它有其用途,您可以使用视图缓存来降低性能.
很难准确地衡量你想要做什么requestAction,但基本的概念是你要求CakePHP处理另一个请求,所以你可以简单地传递requestAction你的应用程序在浏览器的地址栏中键入的任何URL(不包括协议和主机名).如果您想要检索由您的应用程序管理的博客集合:
$blogs = $this->requestAction('/blogs/index');
Run Code Online (Sandbox Code Playgroud)
您也可以requestAction通过传递路由组件数组来调用,方法与传入它们的方式相同HtmlHelper::link.因此,您可以检索博客集合:
$blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index');
Run Code Online (Sandbox Code Playgroud)
在过滤返回的结果集的情况下requestAction,再次通过将条件作为URL或路由组件的一部分传递来完成:
$blogs = $this->requestAction('/blogs/index/author_id:1234');
// or
$blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index', 'author_id' => 1234);
Run Code Online (Sandbox Code Playgroud)
请注意,如果您希望所请求的操作返回值,则需要以不同于标准操作请求的方式处理请求.对于BlogsController::index我上面提到的动作,它可能看起来像这样:
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'));
}
}
}
Run Code Online (Sandbox Code Playgroud)
if检查存在和价值的陈述$this->params['requested']是关键部分.它检查操作是否被调用requestAction.如果是,则返回返回的博客集合Blog::find; 否则,它使集合可用于视图,并允许控制器继续渲染视图.
使用requestAction它来获得您需要的特定结果有很多细微差别,但上面应该为您提供基础知识.查看dogmatic69发布的链接以获取更多文档,当然还有很多关于这个主题的stacko问题.
随意评论任何后续行动!希望这有帮助.