开发者

in view call to controller function in cake php

开发者 https://www.devze.com 2022-12-11 07:04 出处:网络
i need some help which is when i on my index.ctp/view.ctp, i need to call to my controller function to perform some task. what code i can use to perform开发者_如何学运维 this action?

i need some help

which is when i on my index.ctp/view.ctp, i need to call to my controller function to perform some task. what code i can use to perform开发者_如何学运维 this action?

  • i need to call to my controller function, which send in a value (user_id) to the function and get me a certain action. how can i do that? i might calling in a javascript function as well.


If you need to call a Controller function from the View, you're doing it wrong. It's not proper MVC.

Having said that, requestAction would be the proper, albeit slowest way to do so. You could hack around a bit more and get an instance of the Controller from the ClassRegistry. But I'd seriously recommend you to restructure your program flow so you don't need to do this to begin with.


You should probably perform the task in the controller before you get the view. But if you need to do some view work on the data you are displaying you might want to consider making a Helper class.

  1. http://book.cakephp.org/view/101/Creating-Helpers


If your task doesn't generate any output - you might want to consider doing it in the controller before you even get to the view stage.

If your task has some form of output - use requestAction with a view Element

  1. http://bakery.cakephp.org/articles/view/creating-reusable-elements-with-requestaction

That link should be a good starting point. There are also good posts by Mark Story on his blog that detail the actual performance of requestAction and it really isn't that bad if you don't abuse it all over the place.

  1. http://mark-story.com/posts/view/how-using-requestaction-increased-performance-on-my-site
  2. http://mark-story.com/nodes/view/reducing-requestaction-use-in-your-cakephp-sites-with-fat-models

If you really need to trigger some sort of logic in a predictable way and that logic might happen in more than one place you can also use an event observer pattern to trigger the controller action you need to run.

  1. http://cakealot.com/2009/04/eventful-a-cakephp-event-system/
0

精彩评论

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