Search code examples
phplaravelregistration

Customize Laravel registration process


I want to customize the Laravel registration process. User is unable to directly click on the link and register, instead of that I send a link to the user for registration. eg: http://127.0.0.1:8000/register?id=123 If the ID is available in customer table then get the email address against the id and redirect to Registration screen with email address.

How to change web.php file to accept parameters? Auth::routes();

How to change the Register related controllers to accept parameters and pass values to view?

Register screen


Solution

  • Your question isnt clear enough but i'll try.

    First you have to turn off registration from the auth like so Auth::routes(['register' => false]);, this way users wont get access to the registration route even if the typed it in the url.

    After you have done this go to your Auth\RegisterController class and take a peek into the RedirectsUsers trait that it uses, you will find a register method that looks like:

        /**
         * Handle a registration request for the application.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function register(Request $request)
        {
            $this->validator($request->all())->validate();
    
            event(new Registered($user = $this->create($request->all())));
    
            $this->guard()->login($user);
    
            if ($response = $this->registered($request, $user)) {
                return $response;
            }
    
            return $request->wantsJson()
                        ? new Response('', 201)
                        : redirect($this->redirectPath());
        }
    

    This is where the registration logic happens, you can customize it any way you see fit but be careful not to shoot yourself in the foot.

    For the routing you can do one or two things

    e.g 1

    Route::get('/custom/registration', 'RegisterController@register')->name('register');
    
    

    Then you can access your id in the controller like this:

    public function register(Request $request)
    {
        $id = $request->query('id)
    
        // Run your logic...
    }
    

    or

    e.g 2

    Route::get('/custom/registration/{id}', 'RegisterController@register')->name('register');
    
    

    Then you can access your id in the controller like this:

    public function register(Request $request, $id)
    {
        
        // Run your logic...
    }
    

    Which ever way makes sense to you is fine.