从 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方法有一个参数说没有注册,也许你可以向项目提交拉取请求,那就太好了。

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

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

正如马克·戴维森所说,这不可能开箱即用。但这就是我的处理方式。

现在它可能有点矫枉过正,但我传递了一系列需要的东西。如果未传递任何参数,则创建默认路由。

// 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并在注册控制器中更改它.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);