如果用户未登录 Laravel,则重定向到登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28363795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:52:03  来源:igfitidea点击:

Redirect to Login if user not logged in Laravel

laravellaravel-4laravel-routing

提问by rkaartikeyan

i am new to laravel,

我是 Laravel 的新手,

i have code in my controller's __construct like

我在控制器的 __construct 中有代码,例如

if(Auth::check())
{
    return View::make('view_page');
}

return Redirect::route('login')->withInput()->with('errmessage', 'Please Login to access restricted area.');

its working fine, but what i wants is. its really annoying to put these coding in each and every controller, so i wish to put this Verify Auth and redirect to login page in one place, may be in router.phpor filters.php.

它工作正常,但我想要的是。将这些代码放在每个控制器中真的很烦人,所以我希望将此验证身份验证和重定向到登录页面放在一个地方,可能在router.phpfilters.php

I have read some posts in forum as well as in stackoverflow, and added code in filters.phplike below but that's too not working.

我已经阅读了论坛和 中的一些帖子stackoverflow,并在filters.php下面添加了代码,但这也不起作用。

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

Please help me to resolve this issue.

请帮我解决这个问题。

回答by lukasgeiter

Laravel 5.4

Laravel 5.4

Use the built in authmiddleware.

使用内置的auth中间件。

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

For a single route:

对于单一路线:

Route::get('profile', function () {
    // Only authenticated users may enter...
})->middleware('auth');

Laravel docs

Laravel 文档

Laravel 4 (original answer)

Laravel 4(原始答案)

That's already built in to laravel. See the authfilter in filters.php. Just add the before filter to your routes. Preferably use a group and wrap that around your protected routes:

这已经内置到 Laravel 中了。请参阅 中的auth过滤器filters.php。只需将 before 过滤器添加到您的路线中。最好使用一个组并将其包裹在受保护的路线周围:

Route::group(array('before' => 'auth'), function(){
    // your routes

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

Or for a single route:

或者对于单一路线:

Route::get('/', array('before' => 'auth', 'uses' => 'HomeController@index'));

To change the redirect URL or send messages along, simply edit the filter in filters.phpto your liking.

要更改重定向 URL 或发送消息,只需filters.php根据自己的喜好编辑过滤器。

回答by Kiran Maniya

To avoid code repetition, You can use it in middleware. If you are using the Laravel build in Auth, You can directly use the authmiddleware as given,

为了避免代码重复,您可以在中间件中使用它。如果您在Auth中使用 Laravel 构建,您可以直接使用auth给定的中间件,

Route::group(['middleware' => ['auth']], function() {
   // define your route, route groups here
});

or, for a single route,

或者,对于单一路线,

Route::get('profile', function () {

})->middleware('auth');

If you are building your own, custom Authentication system. You should use the middleware which will check the user is authenticated or not. To create custom middleware, run php artisan make:middleware Middelware_Name_Hereand register the newly created middleware.

如果您正在构建自己的自定义身份验证系统。您应该使用中间件来检查用户是否经过身份验证。要创建自定义中间件,请运行php artisan make:middleware Middelware_Name_Here并注册新创建的中间件。

回答by pranavbapat

It's absolutely correct what other people have replied. This solution is for Laravel 5.4 But just in case, if you have more than one middleware applying to routes, make sure 'auth' middleware comes in the end and not at the start.

其他人的回答完全正确。此解决方案适用于 Laravel 5.4 但以防万一,如果您有多个应用于路由的中间件,请确保 'auth' 中间件出现在最后而不是开始。

Like this:

像这样:

Route::prefix('/admin')->group(function () {
    Route::group(['middleware' => 'CheckUser', 'middleware' => 'auth'], function(){

    });
});