Blocking routes for a particular User type in Laravel 5.3

Asked

Viewed 1,492 times

0

This is my first post here. I’m starting to use Laravel 5.3 to create a system. In this system I will have two types of users (Admin and Client). I would like to block certain routes for the client. I was able to limit clients from viewing certain view items (using roles), but I would like to block routes for more security. How could I tell the system to only accept admin users access the delete route, for example ?

I’m sorry if it’s a half-ass question, but I’m learning and I’m kind of lost my way.

1 answer

2


To control users' access to routes you must use middlewares.

The Laravel documentation explains well how to use : https://laravel.com/docs/5.3/middleware

Example:

You can set in a middleware that only people older than 20 years access certain route. The middleware can be set:

<?php

namespace App\Http\Middleware;
use Closure;

class CheckAge
{
    public function handle($request, Closure $next)
    {
        if ($request->age <= 20) {
            return redirect('home');
        }
        return $next($request);
    }
}

To include on the route:

Route::get('adultos', function () {
    //
})->middleware('auth');

This is a very simple example, I recommend you read all the documentation to really understand how to use and all the usage options.

  • Thanks for the reply. I managed to realize the limitations

Browser other questions tagged

You are not signed in. Login or sign up in order to post.