从 Laravel 身份验证中排除路由

Exclude route from Laravel authentication

在运行php artisan make:auth之后所有需要的路由都在route.php文件中,但是是否可以删除一个(我想删除注册路由)?

目前我有

Route::group(['middleware' => 'web'], function () {
    Route::auth();
});

我知道Route::auth()是添加所有路由的快捷方式。 我应该自己指定路线而不是使用快捷方式吗?

遗憾的是,您无法使用 Route::auth() 的当前实现排除注册。

您必须手动指定所有路线,因此

// 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');

我认为这是一件很常见的事情,如果 auth 方法有一个参数可以说无需注册就更好了,也许您可​​以向项目提交拉取请求。

正如 Mark Davidson 所说,开箱即用是不可能的。但这就是我的处理方式。

现在这可能有点矫枉过正,但我​​传递了一系列所需内容。如果没有传递参数,则创建默认路由。

// 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');
    }
}

对于您的情况,您可能只传递 boolean 作为参数而不是 array。如果布尔值是 true 则不加载 register 路由,否则加载所有内容。

希望对您有所帮助。

我只是 YOLO 并在 RegisterController.php

中更改它
public function __construct()
{
    $this->middleware('guest');
}

至此

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

这使得注册页面需要您登录才能访问。

这是一个技巧。但这是一个很好的技巧。

编辑: 并将其添加到您的播种机中,让生活更轻松:

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

您可以像这样向 /register 路由添加重定向:

<?php

Auth::routes();

// prevent registration, this needs to be after Auth::routes()
Route::redirect('register', '/home', 301);

在新版本中,您可以这样做(在您的 web.php 文件中):

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