Laravel Entrust this package, the control of the role of access to the directory, the idea of what is the point?

Source: Internet
Author: User
Keywords Php laravel
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');    }    // ...}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.