Search code examples
symfonyhttp-redirectfosuserbundlesymfony-routing

FOSUserbundle login form not redirecting to route


I'm using FOSUserBundle and i want to redirect users to homepage after login. I'm using annotations to define my routes and i have named my homepage route "homepage". However, i get the error "no route found for homepage" after login. When i remove the default_target_path and always_use_default_target_path information from my security.yml file (as shown below), the form redirects me to a blank page after login. When i add it back (specifying the route), i get the no route found error.

Here's my routing.yml

app:
resource: "@AppBundle/Controller/"
type:     annotation

fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"

Here's security.yml

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
            require_previous_session: false
            login_path: fos_user_security_login
            check_path: fos_user_security_check
            always_use_default_target_path: false
            default_target_path:            homepage //the name of my route

And here's the default Controller:

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
public function indexAction(Request $request)
   {

      return $this->render('default/index.html.twig');


   }
}

I've cleared cache several times using several commands but still no luck. What could be the issue? Thanks.


Solution

  • Try to use the path of your route rather than its name for your default_target_path,
    e.g. default_target_path: /

    Of course, you should be able to browse the homepage route normally with an authenticated user.

    If it works, execute the php app/console debug:router command.
    If you doesn't see the route homepage, there is a mistake with your route definition.

    My guess it that the route name is not homepage, maybe because of a prefix automatically added due to use of the AppBundle.

    Also, you are setting always_use_default_target_path to false, be sure that your login form doesn't contain a _target_path hidden field.