The setCookie
hook is triggered when sending a cookie to the browser. It passes
a standard object with all cookie properties and expects the same as return value.
Using the setCookie
hook has been deprecated and will no longer work in Contao 6.0. Implement a kernel.response
listener instead.
object $cookie
A stdClass instance that contains the properties of the cookie. See PHP’s setcookie documentation for detailed information.
Return $cookie
or a custom object with all properties.
// src/EventListener/SetCookieListener.php
namespace App\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
#[AsHook('setCookie')]
class SetCookieListener
{
public function __invoke($cookie)
{
// Make sure the cookie is also valid for the whole domain
$cookie->strPath = '/';
return $cookie;
}
}