开发者

CAkePHP calling one controller action from a different controller

In my app, I want to add a user notification each time a user receives a comment on an image or other page. Therefore in my add action in my images controller, I'd like to also call the addNotifications action which is in my Notifications controller. I'm trying to stay away from requestAction based on the warnings, but is there another way?

Workflow is this: New even开发者_运维问答t occurs -> trigger addition of notification in notifications table -> email user that notification exists.


If it's going to be a notification for all sorts of things, then I would consider something in the app_controller as this will make it available across your whole application. Meaning you'll be able to call something like

$this->Notify($user['User']['email'], 'MyNotifyType', 'MyTemplateName');

Then you can deal with the other bits in your app controllers notify function. You might need to add your User model to your app_controller, which could be tricky.

I would try using uses() as this could allow you to add the model and thus pull user data from your app_controller if you wanted to say include the users last login details, username or formal greeting etc. http://api.cakephp.org/class/controller


If you want to call a method that is based on another model, you need to place it in the model class, so in your example in the the Notification model. You can then call it from your Images controller with

$this->Image->Notification->add($params);

if the Models are associated. If they are not, you could either connect them on the fly or go with the previous proposal and add the function in the appController (which is not really perfect, because functions in the AppController should not depend on a certain model but be generic)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜