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.
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\ServiceAnnotation\Hook;
/**
* @Hook("setCookie")
*/
class SetCookieListener
{
public function __invoke($cookie)
{
// Make sure the cookie is also valid for the whole domain
$cookie->strPath = '/';
return $cookie;
}
}