Where to set CakePHP 3 cookie config

2.3k Views Asked by At

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
    ]);

}
1

There are 1 best solutions below

3
On BEST ANSWER

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.