# Loading a Component in AppController or PagesController which one is best?

**URL:** <https://discourse.cakephp.org/t/loading-a-component-in-appcontroller-or-pagescontroller-which-one-is-best/4251>\
**Category:** Performance\
**Tags:** behavior\
**Created:** [May 2, 2018, 11:05am UTC](https://discourse.cakephp.org/t/loading-a-component-in-appcontroller-or-pagescontroller-which-one-is-best/4251 "2018-05-02T11:05:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthikeyan](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@karthikeyan](https://discourse.cakephp.org/u/karthikeyan)\
**Post date:** [May 2, 2018, 11:05am UTC](https://discourse.cakephp.org/t/loading-a-component-in-appcontroller-or-pagescontroller-which-one-is-best/4251/1 "2018-05-02T11:05:05Z")

</div>

Dear Friends,

We are using Friendsofcake’s Crud component in our project, and we came to know that initialize Crud component in AppController and other controllers both are working fine.

**AppController.php**

```
namespace App\Controller;

class AppController extends \Cake\Controller\Controller
{
    use \Crud\Controller\ControllerTrait;

    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'Crud.Index',
                'Crud.Add',
                'Crud.Edit',
                'Crud.View',
                'Crud.Delete'
            ]
        ]);
    }
}

```

**PagesController.php**

```
namespace App\Controller;

class PagesController extends AppController
{
    use \Crud\Controller\ControllerTrait;

    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'Crud.Index',
                'Crud.Add',
                'Crud.Edit',
                'Crud.View',
                'Crud.Delete'
            ]
        ]);
    }
}

```

So please kindly let me know which one is standard? or there is no difference between both? or loading component in PagesController.php will impact on performance?

---

<div class="post-metadata">

**Author:** ![donnidani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/donnidani/32/744_2.png) [@donnidani](https://discourse.cakephp.org/u/donnidani)\
**Post date:** [May 3, 2018, 10:14am UTC](https://discourse.cakephp.org/t/loading-a-component-in-appcontroller-or-pagescontroller-which-one-is-best/4251/2 "2018-05-03T10:14:22Z")

</div>

If component loaded in AppController the component will be loaded in every initialiazation, and it would be accessible Globally from every controller extending AppController .

and If it on PagesController then the component will be initialized in PagesController only. and only PagesController has access to that component.

so the best place to place CrudComponent is in AppController. IMHO

---

<div class="post-metadata">

**Author:** ![brent](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@brent](https://discourse.cakephp.org/u/brent)\
**Post date:** [May 5, 2018, 2:45am UTC](https://discourse.cakephp.org/t/loading-a-component-in-appcontroller-or-pagescontroller-which-one-is-best/4251/3 "2018-05-05T02:45:18Z")

</div>

The only item I’d add from what donnidani has said, as a general rule, only load items that you use when you need them. For example, do not load a component globally if you only use it in a specific plugin.

---

<div class="post-metadata">

**Author:** ![FangerZero](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@FangerZero](https://discourse.cakephp.org/u/FangerZero)\
**Post date:** [October 4, 2018, 4:49pm UTC](https://discourse.cakephp.org/t/loading-a-component-in-appcontroller-or-pagescontroller-which-one-is-best/4251/4 "2018-10-04T16:49:35Z")

</div>

I know this is an older post, but I want to make sure I am understanding correctly.

When loading a component if truly going to use in multiple controllers the AppController initialize() is the place to load it. But if only using it in the PageController Actions and no where else, only load it in PageController initialize().
