简体   繁体   中英

How do I set up referral cookie in Symfony2?

From few weeks I'm learning Symfony2 framework. These days I decided to build a project and one of the main functionality is "referral logic". So my goal is when in the URL is added ? ref =something - a referral cookie to be created and accessed from all controllers.

Can you give me some guidelines or resources? I guess I should use Symfony2's EventDispatcher Component?

BTW I have 1 year experience with Kohana framework. The same logic I developed with a Base Controller which is inherited by rest Controllers. In the Base Controller I wrote method which determine the referral id and it sets the cookie.

You are on the right track. Set the cookie in a response listener:

namespace Cerad\Bundle\CoreBundle\EventListener;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class P3PResponseListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array
        (
            'kernel.response' => array(array('onKernelResponse', 10),)
        );
    }
    public function onKernelResponse(FilterResponseEvent $event)
    {
        // P3P Policy *** SET YOUR COOKIE HERE ***
        $event->getResponse()->headers->set('P3P', 
        'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
    }
}

# services.yml

services:

    cerad_core__p3p_response_listener:
        class:  '%cerad_core__p3p_response_listener__class%'
        tags:
            - { name: kernel.event_subscriber }

http://symfony.com/doc/current/cookbook/service_container/event_listener.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM