T
T
Tarasovych2017-03-10 01:07:07
Laravel
Tarasovych, 2017-03-10 01:07:07

How to create a role and configure rights in laravel?

I need to create a role and restrict access to the admin panel for it. In the database, I added a new value for the role, how can I restrict paths in routes for this role (let's say that attempts to navigate to /admin/... redirect to 404)?

app/Http/Authenticate.php

public function handle($request, Closure $next)
    {
        if ($request->user()->hasRole('admin') {
            return $next($request);    
        }
        else if ($request->user()->hasRole('user'))
        {
            return redirect('404');
        }
        else
            return redirect('/');
    }

this code redirects from admin/ to 404, but the child pages of the admin are accessible
routes.php
//admin routes
    Route::auth();
    Route::group(['middleware' => ['auth']], function () {
        Route::group(['middleware' => ['role']], function () {
            Route::get('/...', function () {
                return redirect(...);
            });
            //пути админки
            });
            //пути админки, которые доступны всем ролям, но при переносе их на уровень выше - ошибка PDOException

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question