I need some help about laravel 5.3 middleware. My problem is the following:
I make an app that has 2 front-end area which are web and admin.
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class AdminMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/');
}
return redirect('admin/');
}
}
and I have 2 controls in App\Http\Controllers\Admin folder
those are LoginController and HomeController
if I enter the localhost:8000/admin/ I go to LoginController and write hello. this is good but if i go localhost:8000/admin/home I see the other string write "home page" but i want to redirect to user admin/login if the user not authenticate but i could not do that.
I added middlewareGroup in Kernel.php
'admin' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
and added routeMiddleware like that
'admin' => \App\Http\Middleware\AdminMiddleware::class,
I want to tell it I don't want role system
I want to make 2 modules
user and admin different.
could user be an admin ? NO ... admin is different from user ... (other people)
you have to add this functions in your file App\Providers\RouteServiceProvider.php
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapAdminRoutes();
//
}
...
protected function mapAdminRoutes()
{
Route::group([
'middleware' => 'admin',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/admin.php');
});
}