【问题标题】:Exclude route from Laravel authentication从 Laravel 身份验证中排除路由
【发布时间】:2016-05-10 00:07:37
【问题描述】:

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

目前我有

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

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

【问题讨论】:

    标签: php laravel laravel-5.2


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        我只是 YOLO 并在 RegisterController.php 中更改它

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

        到这里

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

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

        这是一个黑客。但这是一个很好的 hack。

        编辑: 只需将其添加到播种机中即可让生活更轻松:

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

        【讨论】:

          【解决方案4】:

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

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

          // 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 路由,否则加载所有内容。

          希望对你有帮助。

          【讨论】:

            【解决方案5】:

            很遗憾,您不能使用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 方法有一个参数可以说无需注册,也许您可​​以向项目提交拉取请求。

            【讨论】:

              猜你喜欢
              • 2020-09-09
              • 1970-01-01
              • 1970-01-01
              • 2016-02-05
              • 1970-01-01
              • 1970-01-01
              • 2013-05-16
              • 2016-04-06
              • 1970-01-01
              相关资源
              最近更新 更多