Exclude route from Laravel authentication

不想你离开。 提交于 2019-12-05 04:04:21

Unfortunately you can't exclude register with the current implementation of Route::auth().

You would have to specify all the routes manually so

// Authentication Routes...
$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout');

// Password Reset Routes...
$this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
$this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController@reset');

I think this is a fairly common thing to want to do it would be nice if there was a parameter to the auth method to say without register maybe you could submit a pull-request to the project.

I just YOLO and change this in the RegisterController.php

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

to this

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

This makes the register page require you to be loged in to reach it.

It's a hack. But it is a good hack.

EDIT: and just add this to your seeder to make life easier:

    $u1= new App\User;
    $u1->name = 'Your name';
    $u1->email = 'your@mail.com';
    $u1->password = bcrypt('yourPassword');
    $u1->save();

In the new releases you can do it like so (in your web.php file):

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

As Mark Davidson said, it's not possible out of the box. But this is how I have handled.

Now it might be overkill, but I pass along an array of what is needed. If no parameters are passed, then default routes are created.

// Include the authentication and password routes
Route::auth(['authentication', 'password']);
/**
 * Register the typical authentication routes for an application.
 *
 * @param array $options
 * @return void
 */
public function auth(array $options = [])
{
    if ($options) {
        // Authentication Routes...
        if (in_array('authentication', $options)) {
            $this->get('login', 'Auth\AuthController@showLoginForm');
            $this->post('login', 'Auth\AuthController@login');
            $this->get('logout', 'Auth\AuthController@logout');
        }

        // Registration Routes...
        if (in_array('registration', $options)) {
            $this->get('register', 'Auth\AuthController@showRegistrationForm');
            $this->post('register', 'Auth\AuthController@register');
        }

        // Password Reset Routes...
        if (in_array('password', $options)) {
            $this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
            $this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
            $this->post('password/reset', 'Auth\PasswordController@reset');
        }
    } else {
        // Authentication Routes...
        $this->get('login', 'Auth\AuthController@showLoginForm');
        $this->post('login', 'Auth\AuthController@login');
        $this->get('logout', 'Auth\AuthController@logout');

        // Registration Routes...
        $this->get('register', 'Auth\AuthController@showRegistrationForm');
        $this->post('register', 'Auth\AuthController@register');

        // Password Reset Routes...
        $this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
        $this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
        $this->post('password/reset', 'Auth\PasswordController@reset');
    }
}

For your case, you can probably just pass a boolean as parameter instead of an array. If the boolean is true then do not load the register routes, otherwise load everything.

Hope it helps.

You can add a redirect to the /register route like so:

<?php

Auth::routes();

// prevent registration, this needs to be after Auth::routes()
Route::redirect('register', '/home', 301);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!