如何从 .ctp 文件运行控制器文件的操作?
我想从另一个 .ctp 文件调用一个操作及其控制器文件的 .ctp 文件。 例如,
users_controller.php 有一个名为 list_category() 的操作,我想从 /app/views/pages/index.ctp 文件调用它。不仅要调用list_category,而且还想显示其html输出(我的意思是还应该呈现list_category.ctp)。
谢谢。
I want to call an action along with its .ctp file of a controller file from another .ctp file.
for e.g.
users_controller.php has an action called list_category() and I want to call it from /app/views/pages/index.ctp file. Not just call list_category but also want to show its html output(I mean also list_category.ctp should be rendered).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
创建一个元素,例如
list_category.ctp
。在元素中使用
requestAction
获取数据:在您的控制器中,确保您
返回
您想要的数据。您可以在
list_category.ctp
视图中重用该代码。使用 requestAction 时会产生开销,但通常是 比人们想象的要少。
Create an element, for instance
list_category.ctp
.In the element use
requestAction
to get the data:In your controller make sure you
return
the data you want.You can reuse the code for your
list_category.ctp
view.There is an overhead when using
requestAction
but it is often less than people believe.你能用路由来做到这一点吗?我不确定我脑子里的语法,但我认为您可以指定当您登陆该页面时控制器运行的方法
Can you do that with routing? I'm not sure of the syntax off the top of my head but I think you can specify that method that the controller runs when you land on that page
这似乎是错误的,你想要完成什么?元素怎么样?
It seems wrong, what is it that you're trying to accomplish? How about elements?
如何从主控制器调用控制器,然后将其结果传递到布局。最后使用一个元素在此处渲染输出,并使用该元素在另一个控制器上渲染输出。这样你就不会出现重复的布局。两个控制器仅使用一个元素。
这与 Rails 在“烘焙”布局时创建布局的方式非常相似。它创建在添加和编辑布局中使用的元素的等效项。
How about calling the controller from your main controller, then pass its results to your layout. Finally use an element to render the output there and also use the element to render the output on that other controller too. That way you don't have duplicate layouts. Just one element used by two controllers.
This is very similar to the way Rails creates its layouts when you 'bake' them. It creates an equivalent of a element to use in the add and edit layouts.
这可以通过 requestAction 来完成,但要注意,它很昂贵,你应该小心与它。
This can be done with requestAction, but be aware, that it's expensive and you should be careful with it.