How to disable registration new users in Laravel

后端 未结 27 2788
鱼传尺愫
鱼传尺愫 2020-12-07 10:34

I\'m using Laravel (v5).

I need one user and I\'ve already registered that. Now I want to disable registration for new users. Of course, I need the login form to wor

相关标签:
27条回答
  • 2020-12-07 11:23

    In Laravel 5.4

    You can find all routes which are registered through Auth::routes() in the class \Illuminate\Routing\Router in the method auth()

    it looks like this:

    /**
     * Register the typical authentication routes for an application.
     *
     * @return void
     */
    public function auth()
    {
        // Authentication Routes...
        $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
        $this->post('login', 'Auth\LoginController@login');
        $this->post('logout', 'Auth\LoginController@logout')->name('logout');
    
        // Registration Routes...
        $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
        $this->post('register', 'Auth\RegisterController@register');
    
        // Password Reset Routes...
        $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
        $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
        $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
        $this->post('password/reset', 'Auth\ResetPasswordController@reset');
    }
    

    Just copy the routes that you want/need and you are fine!

    0 讨论(0)
  • 2020-12-07 11:24

    In routes.php, just add the following:

    if (!env('ALLOW_REGISTRATION', false)) {
        Route::any('/register', function() {
            abort(403);
        });
    }
    

    Then you can selectively control whether registration is allowed or not in you .env file.

    0 讨论(0)
  • 2020-12-07 11:25

    This might be new in 5.7, but there is now an options array to the auth method. Simply changing

    Auth::routes();
    

    to

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

    in your routes file after running php artisan make:auth will disable user registration.

    0 讨论(0)
  • 2020-12-07 11:26

    Laravel 5.7 introduced the following functionality:

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

    The currently possible options here are:

    Auth::routes([
      'register' => false, // Registration Routes...
      'reset' => false, // Password Reset Routes...
      'verify' => false, // Email Verification Routes...
    ]);
    

    For older Laravel versions just override showRegistrationForm() and register() methods in

    • AuthController for Laravel 5.0 - 5.4
    • Auth/RegisterController.php for Laravel 5.5
    public function showRegistrationForm()
    {
        return redirect('login');
    }
    
    public function register()
    {
    
    }
    
    0 讨论(0)
  • 2020-12-07 11:27

    The AuthController.php @limonte has overridden is in App\Http\Controllers\Auth, not in the vendor directory, so Git doesn't ignore this change.

    I have added this functions:

    public function register() {
        return redirect('/');
    }
    
    public function showRegistrationForm() {
        return redirect('/');
    }
    

    and it works correctly.

    0 讨论(0)
  • 2020-12-07 11:30

    If you're using Laravel 5.2 and you installed the auth related functionality with php artisan make:auth then your app/Http/routes.php file will include all auth-related routes by simply calling Route::auth().

    The auth() method can be found in vendor/laravel/framework/src/Illuminate/Routing/Router.php. So if you want to do as some people suggest here and disable registration by removing unwanted routes (probably a good idea) then you have to copy the routes you still want from the auth() method and put them in app/Http/routes.php (replacing the call to Route::auth()). So for instance:

    <?php
    // This is app/Http/routes.php
    
    // Authentication Routes...
    Route::get('login', 'Auth\AuthController@showLoginForm');
    Route::post('login', 'Auth\AuthController@login');
    Route::get('logout', 'Auth\AuthController@logout');
    
    // Registration Routes... removed!
    
    // Password Reset Routes...
    Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
    Route::post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
    Route::post('password/reset', 'Auth\PasswordController@reset');
    

    If you're using lower version than 5.2 then it's probably different, I remember things changed quite a bit since 5.0, at some point artisan make:auth was even removed IIRC.

    0 讨论(0)
提交回复
热议问题