Components

    For more information on the components included in CakePHP, check out thechapter for each component:

    Many of the core components require configuration. Some examples of componentsrequiring configuration are andRequest Handling. Configuration for these components,and for components in general, is usually done via in yourController’s initialize() method or via the $components array:

    You can configure components at runtime using the setConfig() method. Often,this is done in your controller’s beforeFilter() method. The above couldalso be expressed as:

    1. public function beforeFilter(EventInterface $event)
    2. {
    3. $this->RequestHandler->setConfig('viewClassMap', ['rss' => 'MyRssView']);
    4. }

    Like helpers, components implement getConfig() and setConfig() methodsto read and write configuration data:

    1. // Read config data.
    2. $this->RequestHandler->getConfig('viewClassMap');
    3.  
    4. // Set config
    5. $this->Csrf->setConfig('cookieName', 'token');

    As with helpers, components will automatically merge their $_defaultConfigproperty with constructor configuration to create the $_config propertywhich is accessible with getConfig() and .

    One common setting to use is the className option, which allows you toalias components. This feature is useful when you want toreplace $this->Auth or another common Component reference with a customimplementation:

    1. // src/Controller/PostsController.php
    2. class PostsController extends AppController
    3. {
    4. public function initialize(): void
    5. {
    6. $this->loadComponent('Auth', [
    7. 'className' => 'MyAuth'
    8. ]);
    9. }
    10. }
    11.  
    12. // src/Controller/Component/MyAuthComponent.php
    13. use Cake\Controller\Component\AuthComponent;
    14.  
    15. class MyAuthComponent extends AuthComponent
    16. {
    17. // Add your code to override the core AuthComponent

    The above would alias MyAuthComponent to $this->Auth in yourcontrollers.

    Note

    You might not need all of your components available on every controlleraction. In situations like this you can load a component at runtime using theloadComponent() method in your controller:

    Note

    Keep in mind that components loaded on the fly will not have missedcallbacks called. If you rely on the beforeFilter or startupcallbacks being called, you may need to call them manually depending on whenyou load your component.

    Once you’ve included some components in your controller, using them is prettysimple. Each component you use is exposed as a property on your controller. Ifyou had loaded up the in your controller, you could access it like so:

    1. class PostsController extends AppController
    2. {
    3. public function initialize(): void
    4. {
    5. parent::initialize();
    6. $this->loadComponent('Flash');
    7. }
    8.  
    9. public function delete()
    10. {
    11. if ($this->Post->delete($this->request->getData('Post.id')) {
    12. $this->Flash->success('Post deleted.');
    13. return $this->redirect(['action' => 'index']);
    14. }
    15. }

    Note

    Since both Models and Components are added to Controllers asproperties they share the same ‘namespace’. Be sure to not give acomponent and a model the same name.

    Suppose our application needs to perform a complex mathematical operation inmany different parts of the application. We could create a component to housethis shared logic for use in many different controllers.

    The first step is to create a new component file and class. Create the file insrc/Controller/Component/MathComponent.php. The basic structure for thecomponent would look something like this:

    1. namespace App\Controller\Component;
    2.  
    3. use Cake\Controller\Component;
    4.  
    5. class MathComponent extends Component
    6. {
    7. public function doComplexOperation($amount1, $amount2)
    8. {
    9. return $amount1 + $amount2;
    10. }
    11. }

    All components must extend Cake\Controller\Component. Failingto do this will trigger an exception.

    Once our component is finished, we can use it in the application’scontrollers by loading it during the controller’s method.Once loaded, the controller will be given a new attribute named after thecomponent, through which we can access an instance of it:

    1. // In a controller
    2. // Make the new component available at $this->Math,
    3. // as well as the standard $this->Csrf
    4. public function initialize(): void
    5. {
    6. parent::initialize();
    7. $this->loadComponent('Math');
    8. $this->loadComponent('Csrf');
    9. }

    When including Components in a Controller you can also declare aset of parameters that will be passed on to the Component’sconstructor. These parameters can then be handled bythe Component:

    The above would pass the array containing precision and randomGenerator toMathComponent::initialize() in the $config parameter.

    Sometimes one of your components may need to use another component.In this case you can include other components in your component the exact sameway you include them in controllers - using the $components var:

    1. // src/Controller/Component/CustomComponent.php
    2. namespace App\Controller\Component;
    3.  
    4. use Cake\Controller\Component;
    5.  
    6. {
    7. // The other component your component uses
    8. public $components = ['Existing'];
    9.  
    10. // Execute any other additional setup for your component.
    11. public function initialize(array $config): void
    12. {
    13. $this->Existing->foo();
    14. }
    15.  
    16. public function bar()
    17. {
    18. // ...
    19. }
    20. }
    21.  
    22. // src/Controller/Component/ExistingComponent.php
    23. namespace App\Controller\Component;
    24.  
    25. use Cake\Controller\Component;
    26.  
    27. class ExistingComponent extends Component
    28. {
    29. public function foo()
    30. {
    31. // ...
    32. }
    33. }

    Note

    In contrast to a component included in a controllerno callbacks will be triggered on a component’s component.

    From within a Component you can access the current controller through theregistry:

    1. $controller = $this->_registry->getController();
    1. $controller = $event->getSubject();

    Components also offer a few request life-cycle callbacks that allow them toaugment the request cycle.

    • beforeFilter(EventInterface $event)
    • Is called before the controller’sbeforeFilter method, but after the controller’s initialize() method.
    • startup(EventInterface $event)
    • Is called after the controller’s beforeFiltermethod but before the controller executes the current actionhandler.
    • beforeRender(EventInterface $event)
    • Is called after the controller executes the requested action’s logic,but before the controller renders views and layout.
    • shutdown(EventInterface $event)
    • Is called before output is sent to the browser.
    • beforeRedirect(EventInterface $event, $url, Response $response)
    • Is invoked when the controller’s redirectmethod is called but before any further action. If this methodreturns false the controller will not continue on to redirect therequest. The $url, and $response parameters allow you to inspect and modifythe location or any other headers in the response.