Laravel 4 Auth始终重定向到登录页面吗? /登录 [英] Laravel 4 Auth redirect always to login page? /login
问题描述
我在我的Web应用程序和routes.php中使用laravel:
I am using laravel for my web application and in routes.php I have:
// admin routes
Route::group(array('before' => 'auth'), function()
{
Route::controller('admin', 'UsersController');
});
我想保护并检查该人是否已登录,但是此代码始终重定向到"/login",我希望它重定向到"admin/login",可以这样做吗?
I want to protect and check if the person is logged in , but this code always redirect to "/login" i want it to redirect to "admin/login" , can this be done?
推荐答案
filters.php
文件中有一个默认的auth
过滤器,应该是这样的:
There is a default auth
filter in the filters.php
file, it should be like this:
Route::filter('auth', function($route, $request)
{
if (Auth::guest()) return Redirect::guest('login'); // /login url
});
此过滤器(如上所示)将检查用户是否未登录,然后将发生重定向,并将用户发送到/login
url
,否则将不会发生任何事情,用户将被发送到请求的页面.
This filter (given above) will check if the user is not logged in then a redirect will occur and user will be sent to /login
url
and otherwise nothing will happen, user will be sent to the requested page.
此外,默认情况下,以下filter
可用,并且此filter
仅检查用户是否已登录,然后默认情况下会将其重定向到/
(主页):
Also, following filter
is available by default and this filter
just checks if the user is already logged in then (s)he will be redirected to /
(home page) by default:
Route::filter('guest', function($route)
{
if (Auth::check()) return Redirect::to('/'); // you may change it to /admin or so
});
此(guest
)过滤器与/login
一起使用,如下所示,因此,如果已登录的用户打算登录,则默认情况下,该用户将被重定向到主页:
This (guest
) filter is used with /login
as given below, so if a logged in user intended to log in then the user will be redirected to home page by default:
Route::get('login', array('before' => 'guest', 'uses' => 'UsersController@getLogin'));
现在,在您的routes.php
文件中,您声明了以下路由:
Now, in your routes.php
file you have following route declared:
Route::group(array('before' => 'auth'), function()
{
Route::controller('admin', 'UsersController');
});
如果一切正常,则此设置应该可以使用.如果注销的用户尝试访问admin
,则该用户将被发送到login
,并且默认情况下这些用户可用并且应该可以使用.
If everything is fine then this setup should work. If a logged out user tries to visit admin
then user will be sent to login
and these are by default available and should work.
这篇关于Laravel 4 Auth始终重定向到登录页面吗? /登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!