在Cake PHP 2.x中 - 如何包含存储在Components文件夹的子文件夹中的组件?

时间:2015-06-30 16:55:32

标签: cakephp cakephp-2.0

例如,在Controller中(这不起作用):

$this->Components->load('api/UserComponent');

...从文件夹结构中调用组件,如下所示:

app/Controller/Component/api/UserComponent.php

3 个答案:

答案 0 :(得分:3)

尝试将此添加到您的引导程序中:当从控制器加载组件时,它会告诉蛋糕也考虑您的子目录

App::build(array(
    'Controller/Component' => array(
        APP.'Controller/Component/api/'
    )
));

之后,您应该能够包含和使用组件,就像它在Component目录中一样

答案 1 :(得分:2)

CakePHP 2.x中的解决方案是两个

立即按App :: build <)添加路径

// Append Path in bootstrap.php
App::build(array(
    'Controller/Component' => array(
        APP . 'Controller'.DS.'Component'.DS.'Reports' . DS,
    )
), App::APPEND);

// Load in controller MyController ....
public $components = array('Paginator', 'Session', 'ReportUsers');
// In controller
$this->ReportUsers->Demo('bla..bla..');

// OR load in fly in action form controller.
$this->ReportUsers = $this->Components->load('ReportUsers');
$this->ReportUsers->Demo('bla..bla..');

或者通过CakePlugin创建一个插件报告,例如:

// Add Plugin in file bootstrap.php
CakePlugin::load('Reports');

// MyController use Example
// Load in controller MyController ....
public $components = array('Paginator', 'Session', 'Reports.ReportUsers');
// In controller
$this->ReportUsers->Demo('bla..bla..');

// OR load in fly in action form controller.
$this->ReportUsers = $this->Components->load('Reports.ReportUsers');
$this->ReportUsers->Demo('bla..bla..');

答案 2 :(得分:0)

您无法将组件放在Component文件夹下的子文件夹中。如果你想更好地组织你的课程,请使用插件。