【问题标题】:Laravel 5 - before auth not workingLaravel 5 - 在身份验证不起作用之前
【发布时间】:2015-07-28 07:05:53
【问题描述】:

今天我尝试对我的应用程序进行一些更改。我尝试首先通过身份验证传递所有页面。我在这个网站上尝试了一个答案。但这没有帮助。请帮忙。这是我在routes.php中的代码

<?php

Route::get('/', function(){
return view('homepage');
});


Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);
Route::group(['before' => 'auth'], function () {
    Route::get('home', function(){
        return \Redirect::to('twitter');
    });

   Route::get('twitter', 'HomeController@index');
   .
   .
   .
   .
});

我的文件中有几条路线。但只有twitter 路由有效。

【问题讨论】:

    标签: php laravel laravel-5 laravel-routing


    【解决方案1】:

    在 laravel 5 中,

    ['before' =&gt; 'auth']

    已弃用。但相反,我应该使用

    ['middleware' =&gt; 'auth']

    【讨论】:

      【解决方案2】:

      Laravel5 有中间件而不是过滤器。我假设您正在尝试仅向访客显示某个页面,为此我们已经内置了访客中间件。

      Route::group(['middleware' => 'guest'], function () {
      Route::get('home', function(){
          return \Redirect::to('twitter');
      });
      
      Route::get('twitter', 'HomeController@index');
      });
      

      如果需要,您还可以在控制器的特定功能上使用中间件,例如

      class MyController extends Controller {
      
      public function __construct()
      {
          //to add auth middleware only on update method 
          $this->middleware('auth', ['only' => 'update'])
          //to add auth middleware on all fucntions expcept login
          $this->middleware('auth', ['except' => 'login'])
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-21
        • 2017-02-04
        • 1970-01-01
        • 2015-02-25
        • 2015-07-19
        • 2013-04-17
        相关资源
        最近更新 更多