cakephp requestAction 方法 查看文件
我如何从我的视图
调用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,使用 requestAction 的性能不是很好,因为它开始了一个全新的调度过程——本质上,您的应用程序正在为用户发出的每个请求处理两个请求。因此,您要尽可能避免使用它。话虽如此,
requestAction
有其用途,您可以使用视图缓存来减轻性能损失。很难准确地判断您想用
requestAction
做什么,但基本概念是您要求 CakePHP 处理另一个请求,因此您可以简单地传递requestAction
任何 URL如果将其输入到浏览器的地址栏中(不包括协议和主机名),您的应用程序将进行处理。如果您想检索由应用程序管理的博客集合:您还可以通过向其传递路由组件数组来调用 requestAction,就像将它们传递到 HtmlHelper 中一样: :链接。因此,您可以这样检索博客集合:
就过滤
requestAction
返回的结果集而言,它也是通过将条件作为 URL 或路由组件的一部分传递来完成的:请注意,如果您想要如果您请求的操作返回值,则您需要该操作以不同于标准操作请求的方式处理请求。对于我上面提到的
BlogsController::index
操作,它可能看起来像这样: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 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.
将此代码放入您的 ctp 文件中
put this code in your ctp file