从元素访问控制器数据

发布于 2024-08-24 21:31:48 字数 69 浏览 12 评论 0原文

我有一个从布局/default.ctp 文件中调用侧边栏导航的元素,我需要从照片控制器访问有关类别的一些数据。我该怎么做呢?

I have an element for my side bar navigation being called from my layouts/default.ctp file, I need to access some data about categories from my Photos controller. How would I go about doing this?

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

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

发布评论

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

评论(4

︶ ̄淡然 2024-08-31 21:31:48

请小心 requestAction,除非您有效地利用缓存,否则它确实会减慢您的应用程序的速度,因为每次调用它时它都会启动一个全新的请求周期。

特拉维斯·勒鲁 (Travis Leleu) 的答案是标准的做事方式。但是,如果您必须将数据(出于可移植性或任何原因)导入到元素中,那么 requestAction 就不是正确的选择。

由于您没有执行控制器中必须存在的任何业务逻辑,因此我强烈建议您将模型类作为单例导入并实例化到元素中。您可以使用 ClassRegistry::init(); 来做到这一点

$Photo = ClassRegistry::init('Photo');
$Photo->find('all');

如果您需要对数据进行任何额外的处理,您应该在模型本身中使用 Cakes afterFind 回调或在照片模型中创建自定义方法:

class Photo extends AppModel {

    function customFind () {
        $photos = $this->find('all');
        foreach ($photos as $photo) {
            //processing code here...
        }
    }

}

然后在您的元素中调用它:

$Photo = ClassRegistry::init('Photo');
$Photo->customFind();

基本上是我想要了解的内容这是 requestAction 适合的唯一情况是您需要执行重定向或使用组件之类的操作。

如果您只是检索和/或操作数据,那么它就属于模型。

Be careful with requestAction unless you make effective use of caching it can really slow down your application as it starts a whole new request cycle every time you call it.

Travis Leleu's answer would be the standard way of doing things. But, if you must import the data (for portability or whatever reason) into the element then requestAction is not the way to go.

As you are not performing any business logic that must be in the controller I strongly recommend you import and instantiate the model class as a singleton into your element. You can do this using ClassRegistry::init();

$Photo = ClassRegistry::init('Photo');
$Photo->find('all');

If you need to do any additional processing of the data you should do that in the model itself either using Cakes afterFind callback or making a custom method in your Photo model:

class Photo extends AppModel {

    function customFind () {
        $photos = $this->find('all');
        foreach ($photos as $photo) {
            //processing code here...
        }
    }

}

Then call it in your element:

$Photo = ClassRegistry::init('Photo');
$Photo->customFind();

Basically what I'm trying to get across here is the only situation where requestAction is appropriate is where you need to do things like redirects or using components.

If you a simply retrieving and/or manipulating data then it belongs in the model.

初懵 2024-08-31 21:31:48

你可以把你的layous/default.ctp当作一个普通的模板,放在

<?php echo $this->element('your element'); ?>

你需要的地方。

顺便说一句,使用:

$data = $this->requestAction('controller/action');

访问数据

You can just regard your layous/default.ctp as a normal template,and put

<?php echo $this->element('your element'); ?>

where you need it.

b.t.w,use:

$data = $this->requestAction('controller/action');

to access the data

两仪 2024-08-31 21:31:48

您可以将数据发送到您的元素。例如,在 default.ctp:

<?php echo $this->element('side_nav', $your_data); ?>

和 side_nav.ctp 中,您可以处理该数据。

You can send data to your element. For example in your default.ctp:

<?php echo $this->element('side_nav', $your_data); ?>

and in your side_nav.ctp you can process that data.

高速公鹿 2024-08-31 21:31:48

你为什么不按照标准的 Cake 约定来做呢?

在控制器中,

$this->set( 'categories', $this->Photos->find(...) );

Why wouldn't you just do it the standard Cake convention for this?

In the controller,

$this->set( 'categories', $this->Photos->find(...) );

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