Symfony - Setting Cookie onKernelRequest

i want to be able to set a Cookie onKernelRequest Method, but the cookie is not beeing set, Everything else is working fine, what am missing here? What i want to achieve is that if the user is not logged in and doesnt have the cookie he should see the http basic auth headers. If the user is logged in or does have the cookie he has access to the preview domains without having to enter their user credentials in http basic auth.

const AUTH_USER = 'myuser';
const AUTH_PW = 'mypass';

public function sendAuthorizationHeader()
{
    header('WWW-Authenticate: Basic realm="Preview Domain"');
    header('HTTP/1.0 401 Unauthorized');
    die();
}

public function onKernelRequest(GetResponseEvent $event)
{
    if (!$event->isMasterRequest()) {
        return;
    }

    $request = $event->getRequest();
    $host = $request->getHost();
    $loginSuccessful = false;

    // check if we are on a preview domain
    if (preg_match('/^preview-\d+/', $host)) {
        $user = $request->getUser();
        $cookie = $request->cookies->get('preview_user');
        $phpAuthUser = $request->server->get('PHP_AUTH_USER');
        $phpAuthPw = $request->server->get('PHP_AUTH_PW');

        if (isset($phpAuthUser) && isset($phpAuthPw)) {
            if ($phpAuthUser == self::AUTH_USER && $phpAuthPw == self::AUTH_PW) {
                $loginSuccessful = true;
            }
        } else if ($user === null && $cookie === null) {
            $this->sendAuthorizationHeader();
        }

        if (!$loginSuccessful) {
            $this->sendAuthorizationHeader();
        } else {
            $cookie = new Cookie('preview_user', true, 86400, '/', null, false, false);

            $response = new Response();
            $response->headers->setCookie($cookie);
            $response->sendHeaders();
        }
    }
}

Answers


Setting a cookie on a response object doesn't do anything but adding a cookie to that request. You need to return the same response object, so Symfony renders it back to the client. Rendering it yourself is not a good idea, as there might be contents send later and it's not really testable.

It's easier done in a kernel.response event listener, since you already have a response there. Remember to use the response that your application creates. Do not create it yourself.

If you set the cookie based on logic that should also be available during the request, you can split it into two event listener methods. One will set a request attribute on kernel.request, and the other one will set the cookie on the response on kernel.response:

public function onKernelRequest(GetResponseEvent $event)
{
    // your logic goes here. calculate the $result
    // ...

    $event->getRequest()->attributes->set('my_result', $result);
}

public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();
    $request = $event->getRequest();

    $myResult = $request->attributes->get('my_result');
    $cookie = new Cookie(/* ... */);

    $response->headers->setCookie($cookie);
}

Need Your Help

unable to clear text field in autcomplete

jquery autocomplete

I am trying to add autocomplete in my site. Here the autocomplete is working correctly but I just want to clear that field (searching field) after searching for an item, but it is not happening.

Cloud for Flex, Java, mongoDb?

java flex java-ee mongodb blazeds

I am about to develop my masters project using Flex as front end, BlazeDs, Java Web Services and MongoDB in the backend. I am looking to deploy and manage it on a cloud. (The application analyzes