Search code examples
cakephp-3.0cakephp-3.1

Where to set CakePHP 3 cookie config


I want to set the config for the cookie component but I am unsure where to add the code.

Do I set it in the AppController or the bootstrap?

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

    $this->loadComponent('Csrf');

    $this->Cookie->config([
        'httpOnly' => true
    ]);

}

Solution

  • According to http://book.cakephp.org/3.0/en/controllers/components.html#configuring-components

    Some examples of components requiring configuration are Authentication and Cookie. Configuration for these components, and for components in general, is usually done via loadComponent() in your Controller’s initialize() method or via the $components array.

    Assuming that you need to configure it globally, you should place the configurationcode into the initialize() of the AppController.

    If you want to override the configuration at runtime, you can place the code into the beforeFilter() of a controller.