How to disable registration new users in Laravel

后端 未结 27 2789
鱼传尺愫
鱼传尺愫 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:15

    Set Register route false in your web.php.

    Auth::routes(['register' => false]);
    
    0 讨论(0)
  • 2020-12-07 11:16

    As of Laravel 5.7 you can pass an array of options to Auth::routes(). You can then disable the register routes with:

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

    You can see how this works from the source code: src/Illuminate/Routing/Router.php.

    0 讨论(0)
  • I guess this would rather be a better solution.

    Override the following methods as below mentioned in

    App\Http\Controller\Auth\RegisterController.php

    use Illuminate\Http\Response;
    
    .
    .
    .
    
    public function showRegistrationForm()
    {
        abort(Response::HTTP_NOT_FOUND);
    }
    
    public function register(Request $request)
    {
        abort(Response::HTTP_NOT_FOUND);
    }
    
    0 讨论(0)
  • 2020-12-07 11:17

    I had to use:

    public function getRegister()
    {
        return redirect('/');
    }
    

    Using Redirect::to() gave me an error:

    Class 'App\Http\Controllers\Auth\Redirect' not found
    
    0 讨论(0)
  • 2020-12-07 11:19

    The following method works great:

    Copy all the routes from /vendor/laravel/framework/src/Illuminate/Routing/Router.php and paste it into web.php and comment out or delete Auth::routes().

    Then setup a conditional to enable and disable registration from .env. Duplicate the 503.blade.php file in views/errors and create a 403 forbidden or whatever you like.

    Add ALLOW_USER_REGISTRATION= to .env and control user registration by setting its value to true or false.

    Now you have full control of routes and Vendor files remain untouched.

    web.php

    //Auth::routes();
    
    // Authentication Routes...
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout');
    
    // Registration Routes...
    if (env('ALLOW_USER_REGISTRATION', true))
    {
        Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
        Route::post('register', 'Auth\RegisterController@register');
    }
    else
    {
        Route::match(['get','post'], 'register', function () {
            return view('errors.403');
        })->name('register');
    }
    
    // Password Reset Routes...
    Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'Auth\ResetPasswordController@reset');
    

    This is a combination of some previous answers notably Rafal G. and Daniel Centore.

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

    In Laravel 5.5

    Working on a similar issue and setting the middleware argument from guest to 'auth' seemed like a more elegant solution.

    Edit File: app->http->Controllers->Auth->RegisterController.php

    public function __construct()
    {
         //replace this
         //$this->middleware('guest');
    
         //with this argument.
           $this->middleware('auth');
    }
    

    I could be wrong though...but it seems more slick than editing the routing with more lines and less shity than simply redirecting the page...at least in this instance, wanting to lock down the registration for guests.

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