Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Symfony 3 >

TargetPathTrait: Redirect to Previous Page

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We now have control over where the user goes after registering. But... it's not as awesome as it could be. Let me show you why.

First, look at my app/config/security.yml file. In order to access any URL that start with /admin, you need to be logged in. For example, if I go to /admin/genus, it sends me to the login page.

Thanks to Symfony's form_login system, if we logged in, it would automatically redirect us back to /admin/genus... which is awesome! That's clearly where the user wants to go.

But what if I instead clicked a link to register and submitted that form? Shouldn't that also redirect me back to /admin/genus after? Yea, that would be way better: I want to keep the user's experience really smooth.

How can we do this?

Using TargetPathTrait

Guess what? It's almost effortless, thanks to a trait that was added in Symfony 3.1: TargetPathTrait. In your subscriber, use TargetPathTrait. Then, down in onRegistrationSuccess, add $url = $this->getTargetPath() - a method provided by that trait. Pass this $event->getRequest()->getSession() and for the "provider key" argument, pass main. Provider key is a fancy term for your firewall's name.

... lines 2 - 11
class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
use TargetPathTrait;
... lines 15 - 22
public function onRegistrationSuccess(FormEvent $event)
// main is your firewall's name
$url = $this->getTargetPath($event->getRequest()->getSession(), 'main');
... lines 27 - 33
... lines 35 - 41

What's going on here? Well, whenever you try to access a secured page anonymously, Symfony stores that URL somewhere in the session before redirecting you to the login page. Then form_login uses that to redirect you after you authenticate. The TargetPathTrait is just a shortcut for us to read that same key from the session.

If $url is empty - it means the user went directly to the registration page. No worries! Just send them to the homepage.

... lines 2 - 11
class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
... lines 14 - 22
public function onRegistrationSuccess(FormEvent $event)
... lines 25 - 27
if (!$url) {
$url = $this->router->generate('homepage');
... lines 31 - 33
... lines 35 - 41

Let's try the entire flow. I'll go back to /admin/genus: it redirects me to the login page and sets that session key behind the scenes. Then, I'll manually type in /register - but pretend like we clicked a link. Register as aquanaut6, password turtles.

Booya! Logged in and on the /admin/genus page. That's a kick butt registration form.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.3.*", // v3.3.18
        "doctrine/orm": "^2.5", // v2.7.0
        "doctrine/doctrine-bundle": "^1.6", // 1.10.3
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.5
        "symfony/swiftmailer-bundle": "^2.3", // v2.5.4
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.3.0
        "sensio/distribution-bundle": "^5.0", // v5.0.18
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.25
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "knplabs/knp-markdown-bundle": "^1.4", // 1.5.1
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "friendsofsymfony/user-bundle": "^2.0" // v2.0.0
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.4
        "symfony/phpunit-bridge": "^3.0", // v3.2.7
        "nelmio/alice": "^2.1", // v2.3.1
        "doctrine/doctrine-fixtures-bundle": "^2.3", // v2.4.1
        "symfony/web-server-bundle": "^3.3"