1

我想制作一个模式路由,在用户未登录时将其重定向到登录页面。我搜索但找不到解决方案。一如既往,Laravel 的文档毫无用处!

我有这个在我的filter.php

Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('login');
});

Route::filter('auth.basic', function()
{
return Auth::basic();
});

而这条路线在我routes.php

Route::when('/*', 'auth' );

但它不起作用。我怎样才能做到这一点?

4

1 回答 1

1

Change the route declaration for login like this

Route::get( '/login', array('as' => 'login', 'uses' => 'UserController@getLogin') );

In your filters.php use

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::route('login');
});

So, you can use

Route::when('*', 'auth' );

You can also use

Route::group(array('before' => 'auth'), function ()
{
    // Define all routes here with auth
    Route::get('my_first_route', function(){ /... });
    Route::post('my_second_route', 'MyController@myAction');
});
于 2013-10-19T17:32:36.607 回答