假设我有不同的用户可以访问某些页面。页面几乎相同,但有一些小的更改,比如删除按钮不是为用户显示的,而是为管理员显示的。。
如果我有一个名为DashboardController的控制器,它有一个方法索引,显示一些与用户类型相关的信息。
我有两种方法,一种是制作不同的控制器,比如:
Admin'DashboardController
User'DashboardController
但我的路线是:localhost/admin/dashboard
和localhost/user/dashboard
另一种方法是制作一个名为DashboardController的控制器,并在那里检查用户的类型。
那么,哪种方法更好呢?有没有更好的方法来制作一个URL而不是前缀呢?
我建议看一下视图作曲家:http://laravel.com/docs/4.2/responses#view-作曲家
视图编辑器可以将额外的逻辑绑定到视图。这可以删除一些双重代码,或者在您的情况下,可以验证经过身份验证的用户并将布尔值绑定到视图。
快速示例:
// DashboardComposer.php
class DashboardComposer {
public function compose($view)
{
$user = Auth::user();
$isAdmin = $user->admin; // The attribute 'admin' would be a boolean.
$view->with(
'showDelete',
$isAdmin
);
}
}
在您的刀片视图中,您将检查此状态:
// dashboard.blade.php
@if($isAdmin)
<button>Delete</button>
@endif
请注意,这不会保护您的"删除"端点!在这些路由上添加一个before过滤器就足够了。
// routes.php
Route::delete('resource', [
'as' => 'resource.delete',
'uses' => 'DashboardController@delete',
'before' => 'admin' // This would call a custom filter.
]);
最后,自定义过滤器看起来几乎与视图编辑器相同。
// filters.php
Route::filter('admin', function () {
$user = Auth::user();
return $user->admin; // The attribute 'admin' would be a boolean.
});
最后一点是,我把视图编辑器放在了一个单独的类中,这样我可以更好地组织代码。该过滤器与所有其他过滤器放在同一个文件中,但也可以使用composer执行相同的操作:http://laravel.com/docs/4.2/routing#route-过滤器
看看"筛选类"。
我已经像一样解决了这个问题
routes.php
Route::get('dashboard', ['middleware' => 'auth', 'uses' => 'DashboardController@index']);
// Admin
Route::group([ 'middleware' => 'toegang:admin' ], function ()
{
Route::get('dashboard/projecten', 'ProjectController@index');
Route::get('dashboard/groepen', 'GroepController@index');
Route::get('dashboard/periode', 'PeriodeController@index');
Route::get('dashboard/producten', 'ProductController@index');
Route::get('dashboard/gebruikers', 'UserController@index');
Route::get('dashboard/scoring_rubrics', 'ScroingRubricsController@index');
Route::get('dashboard/pos', 'PosController@index');
Route::get('dashboard/project_status', 'ProjectStatusController@index');
Route::get('dashboard/beoordeling', 'BeoordelingController@index');
});
// Student
Route::group([ 'middleware' => 'toegang:student' ], function ()
{
Route::get('dashboard/project_status_student', 'ProjectStatusStudentController@index');
Route::get('dashboard/account', 'AccountStudentController@index');
});
我有多个管理员/学生角色,他们使用一个控制器DashBoardController。
SO要访问仪表板,管理员或学生都必须具有访问仪表板主页的权限。对于管理员和学生的特定页面,我使用两个路由组。
dashboardcontroller@index
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*
* @return View View
*/
public function index()
{
$autorisatie = Auth::user()->autorisatie();
return view('dashboard.home', compact('autorisatie'));
}
在仪表板控制器中,我将通过调用autorisatie()从用户那里获得角色
用户模型中的自回归方法
public function autorisatie($rol = null)
{
$autorisatie = DB::table('autorisatie')
->select('rol')
->where('autorisatieID', Auth::user()->autorisatieID)
->first();
// Check als de opgegeven role in de routes hetzelfde is
// Als de ingelogde user
if($rol)
{
// Return true als $autorisatie rol hetzelfde is als de
// opgegeven route role
return $autorisatie->rol == $rol;
}
// return false
return $autorisatie->rol;
}
在我看来,我会检查不同的显示数据或页面,比如:
查看仪表板
{{--Extends master page--}}@extends("master.master")
{{--Section for content area--}}
@section("content")
<h1>Dashboard</h1>
<p>
Dashboard - {{$autorisatie}} <br>
@if ($autorisatie == 'admin')
Show admin things.....
@elseif ($autorisatie == 'student')
Show student things...
@endif
</p>