2011-10-05 71 views
2

使用CakePHP,我发现我在控制器操作之间复制了一些代码。我有十几个操作(属于各种控制器),都需要运行相同的查询和相同的10个变量,以便在特定布局中使用。他们还需要以相同的方式处理任何错误并呈现错误页面。CakePHP - 集中控制器逻辑

我知道组​​件旨在集中控制器之间使用的逻辑,但在我的情况下,此逻辑需要访问控制器的set()render()方法。对这种情况的建议方法是什么?

感谢布

回答

0

端了我自己的滚动排序业务逻辑层的就这一个。下面的例子。想法/评论欢迎。

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

把逻辑在你的AppController类控制器应该延长。

退房文档:http://book.cakephp.org/view/957/The-App-Controller

+0

即使逻辑仅被我的控制器的一小部分使用? – Brian

+0

你可以有一个扩展AppController的中间类,然后在那里有你的功能,然后你的控制器将从那个类扩展。例如MyController扩展IntermediateController扩展AppController – kand

+0

是的,除了即使在一个控制器内,只有一个动作可能需要这个功能。如果其他操作需要类似的逻辑,我将无法扩展两个中间控制器。 – Brian