Laravel auth检查所有页面

Mif*_*fas 7 php authentication laravel

我创建了身份验证,它的工作完美.但是检查内页有一些问题.例如,

Route::get('/', array('before' => 'auth' , 'do'=> function(){
return View::make('home.index');
}));
Run Code Online (Sandbox Code Playgroud)

索引页面仅对登录用户可见.但是,每当我进入内页时,例如example.com/products.无需登录即可看到产品页面.

小智 8

有许多方法可以为许多路径应用过滤器.

将旋转放入Route::group()或使用控制器在那里添加过滤器,将其添加到其中Base_Controller以便将其应用于所有.您还可以使用过滤器模式并使用正则表达式,该过滤器将过滤器应用于除您不想要的几个之外的所有过滤器.

文档

路线过滤器:http://laravel.com/docs/routing#route-filters

模式过滤器的示例,其他基本上在文档中.由于在此函数中注册正则表达式的问题方式(*实际上已转换为(.*)),因此这可能是最快但也是最成问题的.

Route::filter('pattern: ^(?!login)*', 'auth');
Run Code Online (Sandbox Code Playgroud)

这将对任何路由应用auth,除了example.com/login.


小智 8

这是我的解决方案.

/**
 * Groups of routes that needs authentication to access.
 */
Route::group(array('before' => 'auth'), function() 
{
    Route::get('user/logout', array(
        'uses' => 'UserController@doLogout',
    ));

    Route::get('/', function() {
        return Redirect::to('dashboard');
    });

    Route::get('dashboard',  array(
        'uses' => 'DashboardController@showIndex',
    ));

    // More Routes

});

// Here Routes that don't need Auth.
Run Code Online (Sandbox Code Playgroud)


mat*_*iit 1

只需检查用户是否在您的视图中登录即可。或限制所有控制器(如果您使用它)或检查路由组,并为整个路由组提供过滤器:http://laravel.com/docs/routing#groups