Framework: redirectTo method not working

Created on 3 Jan 2017  路  6Comments  路  Source: laravel/framework

  • Laravel Version: 5.3.28
  • PHP Version: 5.7.6
  • Database Driver & Version: MySQL

Description:

In the documentation mentioned using the redirectTo method in the Authentication controller can do custom logic redirection. However, that doesn't work.

Steps To Reproduce:

  1. In your LoginController, create a method name redirectTo() and return the path string.
  2. Login your application. It will always point to /home.

Work Around:

This is the workaround and should be fix on the next version.

  1. /vendor/laravel/framework/src/Illuminate/Foundation/Auth/RedirectsUser.php
  2. Modify the redirectPath method:
    public function redirectPath()
    {
        if ( method_exists($this, 'redirectTo') ) {
            return $this->redirectTo();
        } else {
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
        }
    }

Most helpful comment

I'm having this same issue:

In your LoginController, create a method name redirectTo() and return the path string.
Login your application. It will always point to /home

It seems to be triggering the RedirectIfAuthenticated middleware:

public function handle($request, Closure $next, $guard = null)
    {
        if(Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }

All 6 comments

I don't see where's the problem, the original implementation works fine:

public function redirectPath()
{
    if (method_exists($this, 'redirectTo')) {
        return $this->redirectTo();
    }

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

If a method exists it'll use it other wise the property will be used.

I'm having this same issue:

In your LoginController, create a method name redirectTo() and return the path string.
Login your application. It will always point to /home

It seems to be triggering the RedirectIfAuthenticated middleware:

public function handle($request, Closure $next, $guard = null)
    {
        if(Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }

The problem is with documentation, because show this:

If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:

protected function redirectTo()
{
//
}
redirectTo method has higher priority than redirectTo attribute.

Source: https://laravel.com/docs/5.3/authentication

But you need define redirectPath() method actually.

Have same issue with 5.4.13, and the problem solved by @RichieMcMullen solution above.

just use return "/home"; for example do not use redirectTo('home');

If you need more robust customization of the response returned when a user is authenticated, Laravel provides an empty authenticated(Request $request, $user) method that may be overwritten if desired:

So put this method in LoginController.php

protected function authenticated(Request $request, $user)
{
        return redirect()->route('displayNew');
}
Was this page helpful?
0 / 5 - 0 ratings