开发者

CakePHP - centralizing controller logic

开发者 https://www.devze.com 2023-04-10 18:27 出处:网络
Using CakePHP, I am finding that I\'m duplicating so开发者_如何转开发me code between controller actions.I have a dozen or so actions (belonging to various controllers) that all need to run the same qu

Using CakePHP, I am finding that I'm duplicating so开发者_如何转开发me code between controller actions. I have a dozen or so actions (belonging to various controllers) that all need to run the same query and set() the same 10 variables for the use in a particular layout. They also need to handle any errors in the same way and render an error page.

I know that components are intended to centralize logic used among controllers, but in my case, this logic needs access to the set() and render() methods of the controller. What is the suggested approach to this situation?

Thanks, Brian


Put the logic in your AppController class which your controller should extend from.

Check out the docs: http://book.cakephp.org/view/957/The-App-Controller


Ended up rolling my own sort of business logic layer on this one. Example below. Thoughts/comments welcome.

class MyController extends AppController {

  public function my_action() {

     //  The BLL class is specific for this action and gets the entire 
     //  controller so has access to the set() method as well as components.
     $this->Bll = new MyActionLogic($this);
     $this->Bll->do_whatever();
  }
}
0

精彩评论

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