如何从 .ctp 文件运行控制器文件的操作?

发布于 2024-09-30 14:17:17 字数 216 浏览 10 评论 0原文

我想从另一个 .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 技术交流群。

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

发布评论

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

评论(5

叹沉浮 2024-10-07 14:17:17

创建一个元素,例如 list_category.ctp

在元素中使用 requestAction 获取数据:

<?php
    $categories = $this->requestAction('/users/list_categories');
?>

<?php foreach($categories as $category): ?>

    <?php // Your display code goes here ?>

<?php endforeach; ?>

在您的控制器中,确保您返回您想要的数据。

<?php
    function list_categories() {

        return $this->User->Category->find('all');

    }
?>

您可以在 list_category.ctp 视图中重用该代码。

使用 requestAction 时会产生开销,但通常是 比人们想象的要少

Create an element, for instance list_category.ctp.

In the element use requestAction to get the data:

<?php
    $categories = $this->requestAction('/users/list_categories');
?>

<?php foreach($categories as $category): ?>

    <?php // Your display code goes here ?>

<?php endforeach; ?>

In your controller make sure you return the data you want.

<?php
    function list_categories() {

        return $this->User->Category->find('all');

    }
?>

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.

恋竹姑娘 2024-10-07 14:17:17

你能用路由来做到这一点吗?我不确定我脑子里的语法,但我认为您可以指定当您登陆该页面时控制器运行的方法

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

能怎样 2024-10-07 14:17:17

这似乎是错误的,你想要完成什么?元素怎么样?

It seems wrong, what is it that you're trying to accomplish? How about elements?

绾颜 2024-10-07 14:17:17

如何从主控制器调用控制器,然后将其结果传递到布局。最后使用一个元素在此处渲染输出,并使用该元素在另一个控制器上渲染输出。这样你就不会出现重复的布局。两个控制器仅使用一个元素。

这与 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.

北笙凉宸 2024-10-07 14:17:17

这可以通过 requestAction 来完成,但要注意,它很昂贵,你应该小心与它。

This can be done with requestAction, but be aware, that it's expensive and you should be careful with it.

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