How to disable registration new users in Laravel

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

    In order not too change the code as it is, just create a middleware to detect if the request url is url('register'), then redirect to 404 or do wherever.

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

    In Laravel 5.5

    I was trying to accomplish the same problem in Laravel 5.5. Instead of using Auth::routes() in the web.php routes file, I only included the login/logout routes:

    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout');
    
    0 讨论(0)
  • 2020-12-07 11:10

    Heres my solution as of 5.4:

    //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...
    //Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
    //Route::post('register', 'Auth\RegisterController@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');
    

    Notice I've commented out Auth::routes() and the two registration routes.

    Important: you must also make sure you remove all instances of route('register') in your app.blade layout, or Laravel will throw an error.

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

    On laravel 5.6 and above you can edit in web.php file

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

    and you can make it true if you change your mind, i see it easy this way

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

    This has been mentioned in earlier comments but I would like to clarify that there are multiple ways to access the auth routes in your web.php file in Laravel ^5.7. depending on your version it might look a little different but they achieve the same result.

    First option

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

    Second option

    Auth::routes([
      'register' => false, // Registration Routes...
      'reset' => false, // Password Reset Routes...
      'verify' => false, // Email Verification Routes...
    ]);
    
    0 讨论(0)
  • 2020-12-07 11:14

    Method 1 for version 5.3

    In laravel 5.3 don't have AuthController. to disable register route you should change in constructor of RegisterController like this:

    You can change form:

    public function __construct()
    {
    
        $this->middleware('guest');
    
    }
    

    to:

    use Illuminate\Support\Facades\Redirect;
    
    public function __construct()
    {
    
        Redirect::to('/')->send();
    
    }
    

    Note: for use Redirect don't forget to user Redirect; So user access to https://host_name/register it's redirect to "/".

    Method 2 for version 5.3

    When we use php artisan make:auth it's added Auth::route(); automatically. Please Override Route in /routes/web.php. You can change it's like this: * you need to comment this line: Auth::routes();

        <?php
    
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | This file is where you may define all of the routes that are handled
    | by your application. Just tell Laravel the URIs it should respond
    | to using a Closure or controller method. Build something great!
    |
    */
    
    
    // Auth::routes();
    Route::get('/login', 'Auth\LoginController@showLoginForm' );
    Route::post('/login', 'Auth\LoginController@login');
    Route::post('/logout', 'Auth\LoginController@logout');
    
    Route::get('/home', 'HomeController@index');
    

    Thanks! I hope it's can solve your problems.

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