The version being used is laravel5.2, when using entrust this package, control the role of access to the directory, the idea of the main points of thinking about it?
Like what:
The route for the administrator is this:
Route::group(['middleware' => 'web'], function () { Route::auth(); Route::group(['prefix' => 'admin','namespace' => 'Admin'], function () { Route::resource('dashboard', 'DashboardController'); });});
Administrator through http://www.example.com/admin/dashboard/
can access, but not the administrator will not be able to access, what should be done?
Reply content:
The version being used is laravel5.2, when using entrust this package, control the role of access to the directory, the idea of the main points of thinking about it?
Like what:
The route for the administrator is this:
Route::group(['middleware' => 'web'], function () { Route::auth(); Route::group(['prefix' => 'admin','namespace' => 'Admin'], function () { Route::resource('dashboard', 'DashboardController'); });});
Administrator through http://www.example.com/admin/dashboard/
can access, but not the administrator will not be able to access, what should be done?
The first step is to create the admin
middleware
// App\Http\Middleware\AdminMiddleware.phpclass AdminMiddleware{ /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { // 如果当前用户没有管理员角色,返回403 if ( ! $request->user()->hasRole('admin')) { abort(403); } return $next($request); }}
Step two, register the admin
middleware
// App\Http\Kernel.phpprotected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'admin' => \App\Http\Middleware\AdminMiddleware::class,];
The third step is to DashboardController
register the middleware you just created within the constructor admin
// App\Http\Controller\DashboardController.phpclass DashboardController extends Controller{ // ... /** * constructor */ public function __construct() { $this->middleware('admin'); } // ...}