PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 1, 2022

[FIXED] CakePHP 3.x Components in Subfolders

 January 01, 2022     cakephp, cakephp-3.0     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing