本文介绍了我应该如何处理或改进我在Laravel的类似过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个应用程序,我正在Laravel建设,这是我第二次认真使用它。该应用程序使您能够登录并访问访客可能无法访问的某些网站区域。这是每个有注册选项的网站的标准票价,默认情况下,Laravel提供了一些标准的方法来处理他们已经过滤的过滤器: Route :: filter('auth',function() { if(Auth :: guest()) { if(Request :: ajax()) { return Response :: make('Unauthorized',401); } else { return Redirect :: guest('login'); } } }); 我的应用程序更进一步,提供了各种权限(您也可以称它们为级别或角色) ,一个用户可以拥有。类似的,但比说,堆栈溢出复杂得多。我用一个枚举来表示这些级别,用更高的数字提供更多访问其他人可能没有的区域: 抽象类UserRole { const Unauthenticated = 1; const Member = 2; 常数Subscriber = 3; const CharterSubscriber = 4; const Moderator = 5; 常量管理员= 6; $ b因此,我发现自己设置了非常相似的过滤器对方,我觉得我违反DRY因为它。过滤器检查你是否有权限访问某些页面,如下所示: Route :: filter('mustBeMemberOrGreater', function(){ if(Auth :: guest()|| Auth :: user() - > role_id< UserRole :: Member){ return Redirect :: home(); } }); route :: filter('mustBeSubscriberOrGreater',function(){ if(Auth :: guest()|| Auth :: user() - > role_id< UserRole :: Subscriber){ return Redirect :: home(); } }); 如果(Auth :: guest()|| Auth :: user() - > role_id< UserRole :: filter('mustBeModeratorOrGreater',function版主){ return Redirect :: home(); } }); // etc等等 正如您所看到的,都非常相似。 是否有,我可以把这个功能包装成更少的重复?我所知道的是Laravel过滤器,因为我现在正在使用它们。我有什么其他的过滤器设置替代品可供我使用?我甚至应该使用过滤器,这种逻辑应该在我的控制器更好? 所有输入赞赏。 解决方案过滤器绝对是正确的选择,但您可以通过传递参数。像这样: Route :: group(array('before'=>'role:Member'),function() { //路线... }); 以下是设置过滤器的方法: Route :: filter('role',function($ route,$ request,$ value){ $ role = constant('UserRole ::'。$ value) ; if(Auth :: guest()|| Auth :: user() - > role_id< $ role){ return Redirect :: home(); } }); I have an application that I'm building in Laravel, this being the second time I've used it seriously. The application provides you the ability to log in and access certain areas of the site that guests/visitors may not have access to. This is pretty standard fare of every website with a signup option, and Laravel by default provides some standard ways to handle them with their already made filters:// LARAVEL DEFAULT FILTERRoute::filter('auth', function(){ if (Auth::guest()) { if (Request::ajax()) { return Response::make('Unauthorized', 401); } else { return Redirect::guest('login'); } }});My application takes this a step further and provides various permissions (you could also call them levels or roles), that a user can have. Sort of similar, but far less complicated than say, Stack Overflow. I am shimming an enum to represent these levels, with a higher number providing more access to areas others may not have:abstract class UserRole { const Unauthenticated = 1; const Member = 2; const Subscriber = 3; const CharterSubscriber = 4; const Moderator = 5; const Administrator = 6;}Because of this, I've found myself setting up filters which are very similar to each other, and I feel like I'm violating DRY because of it. The filters check if you have the necessary permissions to access certain pages, like so:Route::filter('mustBeMemberOrGreater', function() { if (Auth::guest() || Auth::user()->role_id < UserRole::Member) { return Redirect::home(); }});Route::filter('mustBeSubscriberOrGreater', function() { if (Auth::guest() || Auth::user()->role_id < UserRole::Subscriber) { return Redirect::home(); }});Route::filter('mustBeModeratorOrGreater', function() { if (Auth::guest() || Auth::user()->role_id < UserRole::Moderator) { return Redirect::home(); }});// etc, etcAs you can see, they're all very similar to each other. Is there anyway I can wrap this functionality up into something less duplicative? All I know of is Laravel filters as I'm using them now. What other filter setup alternatives do I have at my disposal? Should I even be using filters, should this sort of logic be better in my Controllers?All input is appreciated. 解决方案 Filters are definitely the right choice, but you can make them more dynamic by passing parameters. Like so:Route::group(array('before' => 'role:Member'), function(){ // routes...});Here's how you set up that filter:Route::filter('role', function($route, $request, $value){ $role = constant('UserRole::'.$value); if (Auth::guest() || Auth::user()->role_id < $role) { return Redirect::home(); }}); 这篇关于我应该如何处理或改进我在Laravel的类似过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 04:33