donglugou6652 2013-07-31 10:06
浏览 38
已采纳

cakephp使用组件作为控制器方法

I'm trying to find the best, cleanest way of initialising a method from within another controller. I basically want to record 'likes', 'posts' etc as 'actions' when they happen.

Actions are working fine on submission, but not from outside it's own controller.

In the LikesController, I want to be able to simply go:

$this->Action->add($fields);

But this doesn't work, even if I do $this->loadModel('Action'); beforehand. After reading around it seems that 'components' is the way to go...

So I was wondering how I would achieve this using components. I've got this so far in my LikesController:

public $components = array(
    'RequestHandler','Helper',
        'Action' => array('controller'=>'actions', 'action'=>'add'),
);

But still no joy when I try to call $this->Action->add.

What is the best method of doing this, and how can I set up the component class to work as though it is the Action controller, and able to use its methods?

If I can award REP to the best answer then I will..! Many thanks in advance.

  • 写回答

1条回答 默认 最新

  • du9826 2013-07-31 12:44
    关注

    A component isn't the same as a model. So if you want to do things this way, you'll have to turn your Action model into a Component.

    The component would go into Controller/Component/ActionComponent.php, and then it can be included into your Controllers like this:

    public $components = array(..., 'Action');
    

    The logic in your Component can be similar to that in a Model, but there are some differences. For example, you could still have an Action model, and the component can use that for CRUD and other stuff. You'll have to use ClassRegistry::init() to load a model into your Component though.

    I can't really explain how to do exactly what you want, one reason for that being that I don't know precisely enough what it is you do want. However, I've written my own Component before and you can use it for reference if you like - it's possibly more complicated than yours so you can see how to write methods for it.

    Custom ACL Component

    In that example, once it's included in a Controller, I can simply call $this->CustomAcl->check() anywhere I like.

    I hope this is enough to get you started at least.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路