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

In Cake PHP 2.x - How can I include components that are stored in a subfolder of the Components folder?

例如,从控制器内部(这不起作用):

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

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

app/Controller/Component/api/UserComponent.php

您不能将组件放在 Component 文件夹下的子文件夹中。如果您想更好地组织您的 类 使用插件。

尝试将其添加到您的 bootstrap 中:它会告诉蛋糕在从您的控制器加载组件时也考虑您的子目录

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

之后您应该能够像在 Component 目录

中一样包含和使用组件

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,创建一个插件 Reports 例如:

// 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..');