Routes for users and admins separation Laravel -
i'm trying separate user admin , forbid access of user admin section.
my directories structure is
resources/ -views/ --site/ ---users/ ---admin/
in web.php
i've added
route::get ('/', ['uses' => 'homecontroller@index']); route::auth(); route::group(['middleware' => ['auth'], 'namespace' => 'users', 'prefix' => 'site/users'], function() { // users routes }); route::group(['middleware' => ['auth'], 'namespace' => 'admin', 'prefix' => 'site/admin'], function() { // admin routes route::get ('/admin', ['uses' => 'admincontroller@index', 'before' => 'admin']); });
when log admin , tried open http://example.com/admin i've got
(1/1) notfoundhttpexception
same happens users.
i have column in database is_admin
check , store in session during log in.
what need create example adminmiddleware
in app\http\middleware
.
php artisan make:middleware adminmiddleware
the put inside newly created middleware
public function handle($request, closure $next) { if ($request->user()->type != 'a') { return redirect('home'); } return $next($request); }
based on comment in question store user session , check column is_admin
need change check in if()
. if use auth
this
if (auth::user()->is_admin != 1) {...}
or whatever have in is_admin
column. in routes access this
route::group(['middleware' => 'app\http\middleware\adminmiddleware'], function() { route::get ('/admin', ['uses' => 'admincontroller@index', 'before' => 'admin']); });
Comments
Post a Comment