# CakePHP 4.0 Unable to use custom component in custom plugin

**URL:** <https://discourse.cakephp.org/t/cakephp-4-0-unable-to-use-custom-component-in-custom-plugin/7320>\
**Category:** Plugins\
**Created:** [February 20, 2020, 6:34pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-unable-to-use-custom-component-in-custom-plugin/7320 "2020-02-20T18:34:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvgunsolley](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pvgunsolley](https://discourse.cakephp.org/u/pvgunsolley)\
**Post date:** [February 20, 2020, 6:34pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-unable-to-use-custom-component-in-custom-plugin/7320/1 "2020-02-20T18:34:55Z")

</div>

I’m not sure if I’m doing this correctly. I want to load a component from a plugin in the AppController (or any controller). I believe the correct plugin syntax is simply `$this->loadComponent('Foo.Foo')` for a Foo plugin with a FooComponent.

Here is my plugin directory structure:

├── Foo  
│ ├── README.md  
│ ├── composer.json  
│ ├── phpunit.xml.dist  
│ ├── src  
│ │ ├── Controller  
│ │ │ ├── AppController.php  
│ │ │ └── Component  
│ │ │ └── FooComponent.php  
│ │ └── Plugin.php  
│ ├── tests  
│ │ ├── TestCase  
│ │ │ └── Controller  
│ │ │ └── Component  
│ │ │ └── FooComponentTest.php  
│ │ └── bootstrap.php  
│ └── webroot

When attempting to call an endpoint with a controller that inherits from AppController, I receive the following serialized error:

{  
“message”: “Component class FooComponent could not be found.”,  
“url”: “/controller-that-uses-app-controller”,  
“code”: 500,  
“file”: “path/to/project/vendor/cakephp/cakephp/src/Controller/ComponentRegistry.php”,  
“line”: 111  
}

FooComponent:

namespace Foo\Controller\Component;

use Cake\Controller\Component;  
use Cake\Controller\ComponentRegistry;

/\*\*

- Foo component  
_/  
class FooComponent extends Component  
{  
/_\*
  - Default configuration.
  - 
  - @var array  
\*/  
protected $\_defaultConfig = [];  
}

Foo plugin composer.json psr-4 directive:

```
"autoload": {
    "psr-4": {
        "Foo\\": "src/"
    }
}

```

First party Authorization and Authentication components/plugins work fine.

Im guessing it’s a problem with autoloading/namespaces/directory structure. For now, I’ve removed the plugins and just moved everything back into the main application. After refactoring namespaces, everything is working again for now.

---

<div class="post-metadata">

**Author:** ![Schlaefer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/schlaefer/32/1001_2.png) [@Schlaefer](https://discourse.cakephp.org/u/Schlaefer)\
**Post date:** [February 20, 2020, 8:12pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-unable-to-use-custom-component-in-custom-plugin/7320/2 "2020-02-20T20:12:49Z")

</div>

Did you do a `composer dumpautoload` after changing the composer.json?

---

<div class="post-metadata">

**Author:** ![pvgunsolley](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pvgunsolley](https://discourse.cakephp.org/u/pvgunsolley)\
**Post date:** [February 20, 2020, 8:37pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-unable-to-use-custom-component-in-custom-plugin/7320/3 "2020-02-20T20:37:20Z")

</div>

Thanks for the reply. I didn’t. I’ll refactor the feature into a plugin and try again when done.

---

<div class="post-metadata">

**Author:** ![pvgunsolley](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pvgunsolley](https://discourse.cakephp.org/u/pvgunsolley)\
**Post date:** [February 20, 2020, 8:44pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-unable-to-use-custom-component-in-custom-plugin/7320/4 "2020-02-20T20:44:30Z")

</div>

That seems to have fixed it. I thought psr4 was automatic?

---

<div class="post-metadata">

**Author:** ![Schlaefer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/schlaefer/32/1001_2.png) [@Schlaefer](https://discourse.cakephp.org/u/Schlaefer)\
**Post date:** [February 20, 2020, 9:44pm UTC](https://discourse.cakephp.org/t/cakephp-4-0-unable-to-use-custom-component-in-custom-plugin/7320/5 "2020-02-20T21:44:51Z")

</div>

Composer requires running an appropriate command that trigger it to update its autoload file which establishes known namespace to filepath relationships. After that referencing (sub)items within known namespace-filepath-relationships works automatically.

Performance being a huge reason for that behavior (it would kill performance reevaluating everything on each request).
