Issue
I use Interfaces and Traits to structure more complex Components in CakePHP 3.x. Is it possible to put Component Classes into a seperate Subfolder like:
-Controller
---Component
----Comp1
-----Comp11Component.php
-----Comp12Component.php
-----Comp13Component.php
-----Comp1Interface.php
----Comp2
-----Comp21Component.php
-----Comp22Component.php
-----Comp2Interface.php
...
The Official Documentation tells nothing about this an my tests did fail. Is there potentially a better way to structure complex Components in CakePHP 3.x?
Solution
Putting aside whether such a structure is useful, you can easily use the className
option when loading the component, as long as it is autoloadable, you can load whatever class from wherever you want.
So, given a matching namespace for your components, like App\Controller\Component\Comp1
and App\Controller\Component\Comp2
, you could load your component like this:
$this->loadComponent('Comp1', [
'className' => '\App\Controller\Component\Comp1\Comp11Component'
]);
See also Cookbook > Controllers > Components > Aliasing Components
Answered By - ndm
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.