Reputation:
I'm trying to separate user from admin and forbid access of user to admin section.
My directories structure is
resources/
-views/
--site/
---users/
---admin/
In web.php
I've added this
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 I log as admin and tried to open http://example.com/admin I've got
(1/1) NotFoundHttpException
Same happens with users.
I have column in database is_admin
which I check and store in session during log in.
Upvotes: 1
Views: 1771
Reputation: 3375
What you need is to create for 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 you comment in the question that you store your user session and you check column is_admin
you need to change the check in the if()
. If you use Auth
would be something like this
if (Auth::user()->is_admin != 1) {...}
or whatever you have in is_admin
column. Then in your routes you access it like this
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function()
{
Route::get ('/admin', ['uses' => 'AdminController@index', 'before' => 'admin']);
});
Upvotes: 2
Reputation: 21681
I think you can try this:
Route::group(['middleware' => ['auth'], 'namespace' => 'admin', 'prefix' => 'admin'], function() {
// admin routes
Route::get ('/', ['uses' => 'AdminController@index', 'before' => 'admin']);
});
Hope this work for you !!!
Upvotes: 2