根据一定条件在cake php中加载组件

时间:2012-05-28 07:24:46

标签: cakephp cakephp-1.3

在app_controller.php中

 var $components = array('Session', 'Component1', 'Component2');

我想避免在$this->params['prefix'] == admin时加载所有组件,仅

var $components = array('Session');

就够了..

怎么做?

是否可以不为特定控制器方法加载组件

class PagesController extends AppController
{

    function search()
    {
    // avoid loading  of components ('Component1' and  'Component2') here which is loaded in app_controller  '$components' array
    }

1 个答案:

答案 0 :(得分:0)

然后你可以覆盖构造函数(如果信息很快就可以获得):

public function __construct(CakeRequest $request = null, CakeResponse $response = null) {
    if ($this->params['prefix'] == 'admin') {
        $this->components[] = 'Session';
    }
    parent::__construct($request, $response);
}

但IMO有点愚蠢,只为管理员包含会话组件(因为编程的开销远远超过你通过这样做获得的收益)

PS:还有手册(可能更干净):

public function beforeFilter(){
    parent::beforeFilter();
    if ($this->params['prefix'] == 'admin') {
        $this->Session = $this->Components->load('Session');
    }
}