Disable web middleware for specific routes in laravel 5.2

I want guest users to have access to the homepage, but the laravel built-in authentication process redirects to the login page. How can I give guest users access to the home page?

my routes.php:

Route::group(['middleware' => 'web'], function () { Route::auth(); Route::get('/', ' HomeController@index '); Route::get('/insert', ' HomeController@insertform '); Route::get('/job/{id}', ' JobsController@show '); Route::get('/city/{city}', ' JobsController@city '); Route::post('/insert', ' HomeController@insert '); Route::get('/cityinsert', ' HomeController@cityinsert '); Route::post('/cityinsert', ' HomeController@cityinsertpost '); }); 

and authenticate.php

 class Authenticate { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return redirect()->guest('login'); } } return $next($request); } } 

and this is my kernel.php

 class Kernel extends HttpKernel { /** * The application global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, ]; /** * The application route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \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, ], 'api' => [ 'throttle:60,1', ], ]; /** * The application route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, ]; } 
+5
source share
2 answers

Remove the middleware from the HomeController construct:

 class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { //$this->middleware('auth'); } } 
+6
source

Add exception to middleware declaration in design

 Route::get('/', ' HomeController@index '); 

for the above route, which should be exempted from authentication, you must pass the function name to the middleware, as shown below

 class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth', ['except' => 'index']); } } 
+10
source

Source: https://habr.com/ru/post/1242936/


All Articles