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

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?

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

1 Like

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.

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().