Search code examples
phplaravelentrust

Laravel routes with entrust


I try to Entrust in my Laravel code. At this moment I have users, permisions and roles.

create admin panel where you want to access the "permissions" == "admin - panel"

I wish it was done by the file routes.php

My files: Middleware/EntrustMiddleware.php

class EntrustMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!Entrust::can('admin-panel')) {
        return Redirect::to('home');
        }
        return $next($request);
    }
}

routes.php

Route::get('admin-panel', ['middleware' => ['auth', 'Entrust'], function () {

}]);

I have tried many methods , but still does not work. Can anyone suggest how to set file " routes.php " to access the " admin - panel / 'was only when "permissions" == "admin-panel"

--Edit--

When i'm using this method i get error:

Route::group(['middleware' => ['Entrust']], function () {
    //put your routes here
    Route::get('/admin', 'Admin\AdminController@index');
});

ErrorException in Pipeline.php line 136: call_user_func_array() expects parameter 1 to be a valid callback, class 'Zizaco\Entrust\EntrustFacade' does not have a method 'handle'

--edit2--

['middleware' => ['permission:NAME']]

Now I understand :)

Is Contoller I have to add some extra security or not?


Solution

  • If you are using entrust, for all those routes which you want to allow access for specific roles, you just need to put it in group, that will do the job, being said that, here's how it will look like,

    Route::group(['middleware' => ['add roles name here']], function () {
      //put your routes here
    });