cakephp requestAction 方法 查看文件

发布于 2024-10-30 19:20:02 字数 119 浏览 1 评论 0原文

我如何从我的视图调用requestAction方法到特定的控制器,该方法返回基于我提到的条件?

谢谢 !

How can i call a requestAction method from my view, to specific controller, which returns me results on basis of conditions i mention?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏辞 2024-11-06 19:20:02

一般来说,使用 requestAction 的性能不是很好,因为它开始了一个全新的调度过程——本质上,您的应用程序正在为用户发出的每个请求处理两个请求。因此,您要尽可能避免使用它。话虽如此,requestAction 有其用途,您可以使用视图缓存来减轻性能损失。

很难准确地判断您想用 requestAction 做什么,但基本概念是您要求 CakePHP 处理另一个请求,因此您可以简单地传递 requestAction 任何 URL如果将其输入到浏览器的地址栏中(不包括协议和主机名),您的应用程序将进行处理。如果您想检索由应用程序管理的博客集合:

  $blogs = $this->requestAction('/blogs/index');

您还可以通过向其传递路由组件数组来调用 requestAction,就像将它们传递到 HtmlHelper 中一样: :链接。因此,您可以这样检索博客集合:

  $blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index');

就过滤 requestAction 返回的结果集而言,它也是通过将条件作为 URL 或路由组件的一部分传递来完成的:

  $blogs = $this->requestAction('/blogs/index/author_id:1234');
  // or
  $blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index', 'author_id' => 1234);

请注意,如果您想要如果您请求的操作返回值,则您需要该操作以不同于标准操作请求的方式处理请求。对于我上面提到的 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'));
    }
  }
}

if 语句检查 $this 的存在和值->params['requested'] 是关键部分。它检查该操作是否由 requestAction 调用。如果是,则返回 Blog::find 返回的博客集合;否则,它使集合可用于视图,并允许控制器继续渲染视图。

使用 requestAction 获得您需要的特定结果有许多细微差别,但以上内容应该为您提供基础知识。查看 dogmatic69 发布的链接以获取其他文档,当然还有许多关于该主题的 stacko 问题。

如有任何后续行动,请随时发表评论!希望这有帮助。

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.

沧笙踏歌 2024-11-06 19:20:02
$comments = $this->requestAction('/comments/latest');
foreach($comments as $comment) {
    echo $comment['Comment']['title'];
}

将此代码放入您的 ctp 文件中

$comments = $this->requestAction('/comments/latest');
foreach($comments as $comment) {
    echo $comment['Comment']['title'];
}

put this code in your ctp file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文