Laravel 4 过滤不同角色的组路由
Posted
技术标签:
【中文标题】Laravel 4 过滤不同角色的组路由【英文标题】:Laravel 4 filter group routes for different roles 【发布时间】:2014-11-15 19:54:47 【问题描述】:我有 3 个角色和一个管理面板,我想根据角色在面板上更改链接(和路由),但我不使用任何软件包...我有 1 个通用过滤器、1 个管理过滤器、1 个版主过滤器和 1 个不同的用户过滤器。使用不同的用户过滤器,我想更改控制面板中的所有链接。 这是我的问题:
Route::group(array('before' => 'common'), function()
Route::controller('panel','admin_PanelController');
Route::controller('phone','phneController');
Route::controller('internet','internetController');
Route::controller('message','messageController'); // siteden gelen başvurular
/* admin */
Route::group(array('before' => 'admin'), function()
Route::controller('useroptions','useroptionsController');
);
/* moderator */
Route::group(array('before' => 'mod'), function()
Route::controller('notifications','notificationsController');
);
);
/* different user */
Route::group(array('before' => 'diffuser'), function()
Route::controller('panel','admin_PanelController');
);
我只想向扩散器显示面板并阻止其他通往扩散器的路线,但是当为不同的用户编写这些最终代码时,以上这些部分不起作用!
请帮助我,等待帮助!
我的过滤器
Route::filter('common', function()
if (Auth::guest() )
return Redirect::guest('login');
);
Route::filter('admin', function()
if (Auth::guest() || Auth::user()->type !== "admin" )
return Redirect::guest('login');
);
Route::filter('mod', function()
if (Auth::guest() || Auth::user()->type !== "mod" )
return Redirect::guest('login');
);
Route::filter('diffuser', function()
if (Auth::guest() || Auth::user()->type !== "diffuser" )
return Redirect::guest('login');
);
【问题讨论】:
【参考方案1】:检查“扩散器”的过滤器需要应用于电话/互联网/消息,因为它需要禁止查看这些路线。试试这个:
routes.php
Route::group(array('before' => 'common'), function()
Route::group(array('before' => 'set_links_diffuser'), function()
Route::controller('panel','admin_PanelController');
);
Route::group(array('before' => 'prohibit_diffuser'), function()
Route::controller('phone','phneController'); // check spelling of phne
Route::controller('internet','internetController');
Route::controller('message','messageController');
);
Route::group(array('before' => 'allow_only_admin'), function()
Route::controller('useroptions','useroptionsController');
);
Route::group(array('before' => 'allow_only_mod'), function()
Route::controller('notifications','notificationsController');
);
);
filters.php
Route::filter('common', function()
if (Auth::guest())
return Redirect::guest('login');
);
// no need to repeat Auth::guest() check for these filters,
// since 'common' does that for all
Route::filter('allow_only_admin', function()
if (Auth::user()->type !== "admin" )
return Redirect::guest('login');
);
Route::filter('allow_only_mod', function()
if (Auth::user()->type !== "mod" )
return Redirect::guest('login');
);
Route::filter('prohibit_diffuser', function()
if (Auth::user()->type == "diffuser" )
return Redirect::guest('login');
);
Route::filter('set_links_diffuser', function()
if (Auth::user()->type == "diffuser" )
// do stuff here to change the links on the control panel
);
【讨论】:
以上是关于Laravel 4 过滤不同角色的组路由的主要内容,如果未能解决你的问题,请参考以下文章