Using Symfony2’s AccessDeniedHandlerInterface

This sounds about right. Or, if you’re specifically interested in AccessDeniedException you could also define access_denied_handler within your firewall in security.yml: security: firewalls: my_firewall: # … access_denied_handler: kernel.listener.access_denied.handler # … Then define your service in your services.xml or equivalent: <parameters> <parameter key=”kernel.listener.security.class”>Path\To\Your\Class</parameter> </parameters> <service id=”kernel.listener.access_denied.handler” class=”%kernel.listener.security.class%”> <tag name=”kernel.event_listener” event=”security.kernel_response” method=”handle” /> </service> The handler class: … Read more

What is the best way to notify a user after an access_control rule redirects?

So after quite a bit of research, I found the right way to do this. You’ll need to use an Entry Point service and define it in your firewall configuration. This method will not mess with your default page settings specified in your firewall config for logging in. The Code security.yml: firewalls: main: entry_point: entry_point.user_login … Read more

Symfony2 Routing – route subdomains

Just to point out that this is now added in Symfony v2.2 – http://symfony.com/doc/master/components/routing/hostname_pattern.html. mobile_homepage: path: / host: m.{domain} defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } requirements: domain: %domain% homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }

symfony2 session lifetime

You can set the session expiration time in your config file under the framework section. Mine looks like this: config.yml framework: secret: %secret% charset: UTF-8 error_handler: null csrf_protection: enabled: true router: { resource: “%kernel.root_dir%/config/routing.yml” } validation: { enabled: true, annotations: true } templating: { engines: [‘twig’] } #assets_version: SomeVersionScheme session: default_locale: %locale% cookie_lifetime: 3600 // … Read more

Symfony 2 – how to pass data to formBuilder?

You can give the object you want to use in the __construct() method. Eg : $form = $this ->get(‘form.factory’) ->create(new ApplyStepOneFormType($this->company, $this->ad), $applicant); In your form type : function __construct(\Your\Bundle\Entity\Company $company, \DYB\ConnectBundle\Entity\Ad $ad) { $this->company = $company; $this->ad = $ad; } And then in your form type in buildForm method : $company = $this->company; $builder->add(‘ad’, … Read more

How to cache in Symfony 2?

If you are using Doctrine already just use those cache classes. Add a service to config.yml: services: cache: class: Doctrine\Common\Cache\ApcCache And use it in your controller: if ($fooString = $this->get(‘cache’)->fetch(‘foo’)) { $foo = unserialize($fooString); } else { // do the work $this->get(‘cache’)->save(‘foo’, serialize($foo)); }