我想在cakePHP中加载组件 在此示例中,为json响应
动态加载RequestHandler组件public function xyz() {
$this->components[] ='RequestHandler';
$this->Components->load('RequestHandler');
$this->RequestHandler->initialize($this);
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
初始化函数生成的错误显示未定义。
public function xyz() {
$this->components[] ='RequestHandler';
$this->RequestHandler = $this->Components->load('RequestHandler');
$this->RequestHandler->initialize($this);
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
我也试过
public function xyz() {
$this->RequestHandler = $this->Components->load('RequestHandler');
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
我不能使用这样的组件#$ this-> RequestHandler-> getTime();因为cakephp自动处理json respone。 当我使用http://disecake.localhost/resources/xyz.json
点击上面的代码时{“code”:500,“url”:“/ resources / xyz.json”,“name”:“查看文件” 缺少/var/www/disecake/app/View/Resources/xyz.ctp“。}}
我用的时候
public $components = array( 'RequestHandler');在我的cotroller中而不是输出
{"abc":["hello","world"]}
我认为现在的问题更清楚了。
答案 0 :(得分:11)
CakePHP书中有一节名为“动态加载组件”。
CakePHP 2.x:
http://book.cakephp.org/2.0/en/controllers/components.html#loading-components-on-the-fly
CakePHP 3.x:
http://book.cakephp.org/3.0/en/controllers/components.html#loading-components-on-the-fly
(与你的尝试方式不同)