How can you pass in an (role)argument in a route-group


#1

Hi,

For example I have the following route:

$app->group('', function(){

		// Admin functions
 		$this->get('/dashboard/users', 'DashboardController:getUsers')->setName('auth.dashboard.users');

 })->add(new AdminMiddleware($container));

Perhaps there will be some other roles ‘e.g. moderator, operator, etc’. I would like to make a more general Middleware like RoleMiddleware.

I saw an example In Laravel (something like):

Route::group(['middleware' => 'role:admin'], function () {
      Route::get('admin', function() {
      // do something
     }
})

Is this possible to create in Slim and how would this look?

Then a other question is in bootstrap app I have set-up the following (Globals):

	$view->getEnvironment()->addGlobal('auth', [
				// You will use this method to minimize the SQL queries.
				'check' => $container->auth->check(),
				'user' => $container->auth->user(),
				'admin' => $container->auth->isAdmin(),
		]);

With this i now can check in The twig file something like:

{% if auth.admin %}
            <h6>Admin possibilities</h6>            
        {% else %}
            <p>Not an admin</p>
        {% endif %}

But how could I check if the user is an admin (if I did not set the function to be Global)?


#2

@Keust If I understand your question correctly, you could always add another middleware function to check for user roles:

$app->group('', function(){
    	// Admin functions
 		$this->get('/dashboard/users', 'DashboardController:getUsers')->setName('auth.dashboard.users');
 })
 ->add(new UserRoleMiddleware($container))
 ->add(new AdminMiddleware($container));

The last middleware added is the first one evaluated.