Laravel 5.4 Disable Register Route

前端 未结 13 2308
旧时难觅i
旧时难觅i 2020-12-13 00:09

I am trying to disable the register route on my application which is running in Laravel 5.4.

In my routes file, I have only the

Auth::routes();


        
相关标签:
13条回答
  • 2020-12-13 00:27

    Since Laravel 5.7, a new $options parameter is introduced to the Auth::routes() method; through which you can pass an array to control the generation of the required routes for user-authentication (valid entries can be chosen from the 'register', 'reset', or 'verify' string literals).

    Auth::routes(['register' => false]);
    
    0 讨论(0)
  • 2020-12-13 00:28

    Add this two method to app\Http\Controllers\Auth\RegisterController.php

    public function showRegistrationForm(){
        return redirect('login');
    }
    
    public function register(){
    
    }
    
    0 讨论(0)
  • 2020-12-13 00:28

    Just overwrite your auth showRegistrationForm() method ( place this code inside your Auth/RegisterController )

    public function showRegistrationForm(){
        return redirect()->route('login');
    }
    

    I'm just redirecting my register route to the login route. Here you don't need to append or remove any other code

    0 讨论(0)
  • 2020-12-13 00:29

    You could try this.

    Route::match(['get', 'post'], 'register', function(){
        return redirect('/');
    });
    

    Add those routes just below the Auth::routes() to override the default registration routes. Any request to the /register route will redirect to the baseUrl.

    0 讨论(0)
  • 2020-12-13 00:32

    This is deceptively easy! You just need to override two methods in your app/Http/Controllers/Auth/RegisterController.php Class. See below which will prevent the form from being displayed and most importantly block direct POST requests to your application for registrations..

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        return redirect('login');
    }
    
    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        abort(404);
    }
    
    0 讨论(0)
  • 2020-12-13 00:34

    On my Laravel 5.6 project this method didn't work:

    Auth::routes(['register' => false]);
    

    So I had to use the following method:

    Route::match(['get', 'post'], 'register', function () {
        return abort(403, 'Forbidden');
    })->name('register');
    
    0 讨论(0)
提交回复
热议问题