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

arctushar's avatar

Redirect after Registration like login

Hi I m using auth middleware. I want that if user is not logged in then it will show login form & register form. After login or register it will work. But here by default after login its working and redirecting to coming url. But after register redirecting to home page. plz help

0 likes
3 replies
larafever's avatar

@jrdw, I think Laravel do all that trick with events that can be fired or broadcast, what i found in the ..\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php



//This the default laravel register method 
public function register(Request $request)
    {
    //validates the register form
        $this->validator($request->all())->validate();
    
    //attach new registered user event which can be fired or broadcast
        event(new Registered($user = $this->create($request->all())));
    
    //this automatically login users and enables Auth guard after registration
         $this->guard()->login($user);
    
    /*

this is helper that enables you to do anything with register user or return to a specific URL. By default laravel only redirect to auth middleware after registration since  $this->guard()->login($user); enables Auth guard therefore if you want to redirect to LOGIN, it will be impossible because LOGIN is a Guest guard

*/
        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }

We can override this method to suit our need and still get what Laravel offers by default without changing anything here.. This is how we do it by overriding Register method in RegisterController.php found at ..\app\Http\Controllers\Auth\RegisterController.php

.

use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;



//we define our redirectTo
    protected $redirectTo = '/login';



//create a new method that overrides default register 

    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

       // $this->guard()->login($user);
    //this commented to avoid register user being auto logged in

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }

//we will maintain the default create method but you can tweak it to suit you needs

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

 //we can also play with the registered user object thrown after registration using the registered method because you returned that method or redirectPath method in register method.

protected function registered(Request $request, $user)
    {
        //we can send users account formation email here or anything we want with users even fire that Registered event created earlier

}

Hope it helps

Snapey's avatar

Assuming 5.3

in App\Controllers\Auth\RegisterController.php

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

just change /home to wherever...

1 like

Please or to participate in this conversation.