当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Laravel 4 auth role based page access

问题描述:

What is best way to have page access restricted to authentication role / permit? I was thinking using Auth::user()->role for detection which will be put into every route section. So, is there any better way or best practice for doing this in Laravel 4?

Thank you.

网友答案:

You may try something like this (use a filter for role checking)

Route::filter('role', function()
{
    return Auth::user()->role;
});

Then in your routes, you can use this (role) filter like

Route::get('protectedPage1', array('before'=>'role', function($role) {
    if($role != 'admin') {
        return Redirect::to('home');
    }
}));

Route::get('protectedPage2', array('before'=>'role', function($role) {
    if($role != 'admin') {
        return Redirect::to('home');
    }
}));

Update:

Route::filter('role', function()
{
    if(Auth::user()->role != 'admin') {
        return Redirect::to('home');
    }
});

Route::get('protectedPage1', array('before'=>'role', function() {
    // do your job
}));
分享给朋友:
您可能感兴趣的文章:
随机阅读: