php Laravel 身份验证检查所有页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13745897/
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-08-25 06:00:13  来源:igfitidea点击:

Laravel auth check for all pages

phpauthenticationlaravel

提问by Mifas

I have created the Authentication, and its working perfectly. But there is some problem in checking the inner pages. For example,

我已经创建了身份验证,并且它完美地工作。但是在检查内页时存在一些问题。例如,

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

The index page is only visible for logged in users. But whenever I have go to the inner pages, for example example.com/products. The products page can be visible without log in.

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

采纳答案by matiit

Just check if user is logged in in your views. Or restrict all controller (if you use it) Or check Route Groups, and give a filter to whole group of routes: http://laravel.com/docs/routing#groups

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

回答by Juan Escobar

Here is my solution.

这是我的解决方案。

/**
 * 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.

回答by TLGreg

There are several ways of applying filters for many routes.

有多种方法可以为许多路由应用过滤器。

Putting rotues into Route::group()or if you using controllers add the filter there, add it in the Base_Controllerso it will be applied to all. You can also use filter patterns and use a regex which applies the filter to all except a few you don't want to.

将 rotues 放入Route::group()或如果您使用控制器在那里添加过滤器,将其添加到其中,Base_Controller以便将其应用于所有。您还可以使用过滤器模式并使用正则表达式,将过滤器应用于所有您不想使用的过滤器。

Documentation

文档

Route filters: http://laravel.com/docs/routing#route-filters

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

Example to the pattern filter, as the others are basicly in the docs. This one could be the fastest but also the most problematic because of the problematic way of registering a regex in this function (the *is actually converted into (.*)).

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

Route::filter('pattern: ^(?!login)*', 'auth');

This will apply auth to any route except example.com/login.

这将 auth 应用于除example.com/login.

回答by Khadreal

Route::group(['middleware' => ['auth']], function()
{
    Route::get('list', 'EventsController@index');     
});

Read more on the documentation page: https://laravel.com/docs/5.2/routing#route-groups

在文档页面上阅读更多信息:https: //laravel.com/docs/5.2/routing#route-groups

回答by Jalal Alomari

this code working fine with me

这段代码对我来说很好用

Auth::routes();

Route::group(['middleware' => 'auth'], function () {
    // Authentication Routes...
    Route::get('/', 'HomeController@index')->name('home');

});

回答by Michael J. Calkins

There may be a better way but I take a whitelist approach. Everything is blocked from public except for what the pages I put in this array.

可能有更好的方法,但我采用白名单方法。除了我放在这个数组中的页面之外,所有内容都被禁止公开。

 // config/application.php   
 return array(

    'safe' => array(
        '/',
        'card/form_confirm',
        'record/form_create',
        'card/form_viewer',
        'user/login',
        'user/quick_login',
        'user/register',
        'info/how_it_works',
        'info/pricing',
        'info/faq',
        'info/our_story',
        'invite/accept',
        'user/terms',
        'user/privacy',
        'email/send_email_queue',
        'user/manual_login',
        'checkin/',
        'checkin/activate',
        'system/list',
    ),

// routes.php
Route::filter('before', function()
{
    // Maintenance mode
    if(0) return Response::error( '503' );

    /*
        Secures parts of the application
        from public viewing.
    */
    $location = URI::segment(1) . '/' . URI::segment(2);
    if(Auth::guest() && !in_array( $location, Config::get('application.safe')))
        return Redirect::to( 'user/login' );
});

回答by Brad

Route::filter('pattern: /*', array('name' => 'auth', function()
{
  return View::make('home.index');
}));

回答by Marcos Regis

The same problem can be solved using a BaseController to extends all Controller have must logged user.

使用 BaseController 扩展所有控制器必须登录的用户可以解决同样的问题。

Example:

例子:

class SomeController extends BaseController
{
    public function index() { return view('some.index');}
}

just add a __construct()method to BaseController

只需__construct()向 BaseController添加一个方法

class BaseController extends Controller
{
    protected $redirectTo = '/myIndex'; // Redirect after successfull login

    public function __construct()
    {
        $this->middleware('auth'); // force all controllers extending this to pass auth
    }
}

More info here

更多信息在这里

回答by Hashmat Waziri

It worked for me . take a look at it.

它对我有用。看一看。

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

Route::get('api/getactorinfo/{actorname}', array('uses' =>'ActorController@getActorInfo'));
Route::get('api/getmovieinfo/{moviename}', array('uses' =>'MovieController@getMovieInfo'));
Route::put('api/addactor/{actorname}', array('uses' =>'ActorController@putActor'));
Route::put('api/addmovie/{moviename}/{movieyear}', array('uses' =>'MovieController@putMovie'));
Route::delete('api/deleteactor/{id}', array('uses' =>'ActorController@deleteActor'));
Route::delete('api/deletemovie/{id}', array('uses' =>'MovieController@deleteMovie'));