Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sharlie's avatar

Laravel 5 - Redirect to URL after registration

Hello,

Is it possible to redirect to a specific URL after the user initially creates an account via the default registration flow that comes with laravel 5? I am still learning the structure, but cannot figure out how to change the redirect url.

0 likes
34 replies
bashy's avatar

You have to override it in your controller. Check the current one in the auth trait

protected $redirectTo = '/home';
4 likes
sharlie's avatar

I apologize, but when you say controller, which one do you mean? and where in that controller?

I feel very dumb. I am coming from Cakephp so laravel is a bit different.

1 like
eresources's avatar

I don't believe this was answered correctly and I'm having a similar, specific issue. I want to redirect users to a different billing page when they REGISTER for the first time, separate from the page I send them to when they log in. When I set $redirectTo, it affects both the register and login paths. How can we set this up to only redirect for new user registrations and not for logins?

5 likes
mstnorris's avatar

@sharlie and @eresources http://laravel.com/docs/5.1/authentication#included-authenticating

Now that you have routes and views setup for the included authentication controllers, you are ready to register and authenticate new users for your application. You may simply access your defined routes in a browser. The authentication controllers already contain the logic (via their traits) to authenticate existing users and store new users in the database.

When a user is successfully authenticated, they will be redirected to the /home URI, which you will need to register a route to handle. You can customize the post-authentication redirect location by defining a redirectTo property on the AuthController:

protected $redirectTo = '/dashboard';

absiddiqueLive's avatar

You may overwrite the following variable in your AuthController

protected $loginPath = '/login';
protected $redirectTo = '/home';
protected $redirectAfterLogout = '/';
2 likes
eresources's avatar

I appreciate the responses, but if you try either of these solutions, neither of them work. The redirectTo variable always supercedes the loginPath variable, therefore not letting you set separate paths for register vs. login. I've found a (bad) solution by hard-coding a redirect only for the postRegister function, as follows:

public function postRegister(Request $request)
    {
        $validator = $this->registrar->validator($request->all());
        if ($validator->fails())
        {
            $this->throwValidationException(
                $request, $validator
            );
        }
        $this->auth->login($this->registrar->create($request->all()));
        return redirect('/illustrations');
    }

There's got to be a better way to do this!

mstnorris's avatar

@eresources just override the postRegister and postLogin methods - add them to your AuthController (don't edit the traits within the vendor directory).

3 likes
eresources's avatar

Awesome idea. Don't know why I didn't just do that. Sometimes just takes another person to remind you. Thanks.

andrewtweber's avatar

Just set it to the post-login URL as the default

protected $redirectTo = '/url-after-login';

and then inside the create function, override it to the post-register URL

protected function create(array $data) {
    $this->redirectTo = '/url-after-register';

    return User::create([...]);
}
5 likes
jaewun's avatar

One way of dynamically changing where someone ends up after authentication is setting the intended session variable...

rieves's avatar

Once a user is authenticated, before the redirect is called there is a call to see if a function called authenticated exists. So you can write this in your AuthController.

protected function authenticated(Request $request, User $user)
{
    return redirect()->intended('/dashboard');
}
1 like
phillipsharring's avatar

Nothing new here. As @rieves suggested, I created an authenticated function in the AuthController that does the 'after login' route, and then set the $redirectTo property to the 'after registration' route. Works like a charm; no editing vendor code.

I have the 'after registration' route going to a nice 'first time' user flow, and I added an inspirational message to the session to display on the default 'after login' route that only shows once on the dashboard after login. A little treat for returning visitors.

App\Http\Controllers\Auth\AuthController.php

// welcome the first time sign-up
protected $redirectPath = 'welcome';

// ...

protected function authenticated(Request $request, User $user)
{
    // get some inspiration
    \Artisan::call('inspire');
    $inspiration = \Artisan::output();
    // redirect to the regular 'after login' route
    return redirect()->route('dashboard')->with('inspire-after-login', $inspiration);
}

In the 'dashboard' blade template...

@if(Session::has('inspire-after-login'))
  <div class="inspire">
    { { session('inspire-after-login', '') } }
  </div>
@endif

Edit: That session line should not have spaces between the curly braces, but somehow that wasn't displaying here...

HTH!

2 likes
getgimphed's avatar

I have multiple users with totally different post login panels. One for teachers and other for students. I made middlewares to separate their views. Middleware checks if a property ( column in database ) named entity is 'student' or 'company' and restricts or permits views accordingly. And yes Guest isnt permitted to either of the post login panel. I am using same table to save logins. (entity column differentiates if its a teacher or student). Now I want to redirect the users to different views. If I alter $redirectTo = "/studentPanel". Middleware acts and teacher login cannot access this . But if I hard code $redirectTo = "/teacherPanel" , then student post login panel is not accesible.

How can set $redirectTo dynamically . I thought of setting in construct method of auth controller.

Tried this.....

public function __construct(Request $request ) {

    $this->middleware('guest', ['except' => 'getLogout']);

    if(Auth::user() && $request->user()->isStudent() )
        $this->redirectTo = "/studentPanel";
    elseif(Auth::user() && $request->user()->isTeacher() )
        $this->redirectTo = "/teacherPanel";
    else
          $this->redirectTo = "/auth/login";
}

Here isStudent() and isCompany are functions in App\User which respond with true or false checking the entity column value in Database.

I thought this way and I am getting error "Call to a member function isStudent() on a non-object"

Snapey's avatar

I have done similar with registered users and registered admins, but approached it differently. I had a single route 'dashboard' and then a dashboard controller that was responsible for testing the column in the user database and rendering the correct view. So, all successful logins go to /dashboard but the view is quite different.

This might be different to your scenario because my dashboard does not have any edit states, it only shows a dashboard with some stats and acts as a redirect to the various functions that the user or admin might need to perform.

intosite's avatar

I am having the same issue which i would like to redirect the user if it's post registration. Tried : protected $redirectTo = '/url-after-login'; Does Not work $this->redirectTo = '/url-after-register'; In create(), Does Not work public function postRegister - overwrite function, i get a error "Undefined property: App\Http\Controllers\Auth\AuthController::$registrar"

Willcwf's avatar

I managed to get it to work with the following changes to my AuthController.php

use AuthenticatesAndRegistersUsers { postRegister as postReg; }

...

/**
     * Overrides the default post register to redirect the user to the appropriate location
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postRegister(Request $request)
    {
        $this->postReg($request);
        return redirect('/teams/create');
    }

in AuthController.php

pd_s's avatar

Override redirectPath() in AuthController:

public function redirectPath() {

    $type = auth()->user()->type;
    if (property_exists($this, 'redirectPath')) {
        return $this->redirectPath;
    }
    $redir_path = '/home';
    if ($type == 0) {
        $redir_path = '/myshares';
    }

    if ($type == 1) {
        $redir_path = '/mycompanies';
    }

    return property_exists($this, 'redirectTo') ? $this->redirectTo : $redir_path;
}
1 like
babusharif's avatar

@absiddiqueLive vai ami chachi register korar por redirectTo ='settings' abong login korar por redirectTo='dashboard'

can it be possible for different redirect path for register and login each?

absiddiqueLive's avatar

@babusharif override postRegister() in AuthController or your custom controller

//default
protected $redirectTo = '/dashboard';

//override

public function postRegister(Request $request) {
    protected $redirectTo = '/settings';
    //.......
}
Snapey's avatar

@babusharif

can it be possible for different redirect path for register and login each?

I tackle it by not having a login path. !!

When the user clicks the login button, I direct them immediately to their dashboard (the login button has the URL of /dashboard). If they are not logged in then the middleware catches the request and redirects to login. The user then logs in and gets sent to the 'intended' route - their dashboard.

1 like
baclap's avatar

As @bashy has mentioned, you'll want to define the $redirectedTo variable in your AuthController. Something like:

class AuthController extends Controller
{
    protected $redirectPath = '/dashboard';

This is also documented in Authentication Quickstart. Unfortunately this approach does not allow you to use a named route. If you want to use a named route you can override your AuthController's inherited redirectPath function. Something like:

class AuthController extends Controller
{

    ...

    public function redirectPath()
    {
        return route('dashboard');
    }
NicolasVH's avatar

Every answer here has a hard coded redirectUri.. Is it possible to do something like a Redirect::back() to the previous page??

Snapey's avatar

There is a thread in here that I contributed to where I suggested pushing previous() into the session . unfortunately I can't find it at the moment

west.crosby's avatar

Solved it modifying the register() method in \Illuminate\Foundation\Auth\RegistersUsers.php from return redirect($this->redirectPath()); to return redirect('[path').

ahmednawazkhan's avatar

how to use a dynamic variable inside redirect method like ""redirect(/user/$user/profile)""

ahmednawazkhan's avatar

how to use a dynamic variable inside redirect method like ""redirect(/user/$user/profile)""

Snapey's avatar

@ahmednawazkhan Why don't you ask your own question, then others will see it as needing answering - especially as your question has nothing to do with this thread.

You have to either concatenate the string or insert the user id into the string

    redirect('/user/' . $user->id . '/profile');

    or

    redirect("/user/{$user->id}/profile");  // double quotes

1 like
Next

Please or to participate in this conversation.