Questions tagged [laravel-middleware]

HTTPl Middleware is a mechanism for filtering HTTP requests that are passing through your application. Middleware allows you to add additional layers around your business logic.

Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

Of course, middleware can be written to perform a variety of tasks besides authentication. A CORS middleware might be responsible for adding the proper headers to all responses leaving your application. A logging middleware might log all incoming requests to your application.

There are several middleware included in the Laravel framework, including middleware for maintenance, authentication, CSRF protection, and more. All of these middleware are located in the app/Http/Middleware directory.

Reference

558 questions
113
votes
13 answers

Laravel Middleware return variable to controller

I am carrying out a permissions check on a user to determine whether they can view a page or not. This involves passing the request through some middleware first. The problem I have is I am duplicating the same database query in the middleware and…
Alex
  • 2,003
  • 4
  • 19
  • 30
70
votes
9 answers

Undefined variable: errors in Laravel

When I want to register a user in my laravel project, the page always says Undefined variable: errors (View: /var/www/resources/views/auth/register.blade.php)" According to the Laravel documentation, $errors should always automatically be…
Anhinga
  • 823
  • 2
  • 7
  • 9
68
votes
5 answers

Laravel 5 Resourceful Routes Plus Middleware

Is it possible to add middleware to all or some items of a resourceful route? For example...
kilrizzy
  • 2,895
  • 6
  • 40
  • 63
32
votes
3 answers

Laravel middleware with multiple roles

I've been running into some issues with Laravel's middleware. Let me tell you the basic idea of what I'm trying to accomplish: Registered users on the site will have one of four roles: Student (default): can access 'index' and 'show'…
Jesse
  • 373
  • 1
  • 4
  • 10
29
votes
4 answers

How to set the Laravel middleware order of execution?

The Laravel 5 documentation describes two ways of assigning Middleware: Assign middleware to the controller's route. Specify middleware within your controller's constructor. However, I realised that any code written in the controllers…
Samuel Shen
  • 451
  • 1
  • 4
  • 9
29
votes
2 answers

Laravel Middleware except with Route::group

I'm trying to create a group Route for the admin section and apply the middleware to all paths except for login and logout. What I have so far is: Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function()…
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61
21
votes
7 answers

Laravel middleware 'except' rule not working

I have a controller with the following in the constructor: $this->middleware('guest', ['except' => [ 'logout', 'auth/facebook', 'auth/facebook/callback', 'auth/facebook/unlink' ] ]); The 'logout' rule (which…
jd182
  • 3,180
  • 6
  • 21
  • 30
20
votes
7 answers

Laravel 5 register middleware from in package service provider

I'm currently developing a package in/for Laravel 5. My package contains a custom middleware and I would like to add it to the $routeMiddlewarearray of the Kernel class from in my package Service Provider. But I can't seem to find a way to do…
19
votes
6 answers

Apply Middleware to all routes except `setup/*` in Laravel 5.4

I'm experimenting with Middleware in my Laravel application. I currently have it set up to run on every route for an authenticated user, however, I want it to ignore any requests that begin with the setup URI. Here is what my CheckOnboarding…
Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
15
votes
1 answer

How to prevent Laravel Routes from being accessed directly (i.e. non-ajax requests)

In my project, I am using Laravel purely as a backend api and all frontend is handled by Angular javascript. At the moment, the Laravel routes can be accessed directly and it will cough out all the data in Json that shows in the browser. I want to…
Neel
  • 9,352
  • 23
  • 87
  • 128
15
votes
2 answers

Laravel - Passing variables from Middleware to controller/route

How can I pass variables from a middleware to a controller or a route that executes such middleware? I saw some post about appending it to the request like this: $request->attributes->add(['key' => $value); also others sugested using…
ecorvo
  • 3,559
  • 4
  • 24
  • 35
15
votes
2 answers

Laravel 5: Middleware before & after into routes

I have two Middlewares: beforeCache & afterCache, boths registered on Kernel. I want to call them into routes in this order: 1. beforeCache 2. myController 3. afterCache If I define a route like this: Route::get('especies/{id}', [ 'middleware'…
14
votes
2 answers

Laravel - How to pass variables to middleware through route group?

This is my route group, Route::group(['middleware' => 'checkUserLevel'], function () { // my routes }); And this is my middleware checkUserLevel, public function handle($request, Closure $next, $level) { …
13
votes
6 answers

How to enable both api and web guard in laravel

Laravel 5.7 PHP 7.2.10 Currently I am able to use any one of web and api guards, is there any way to allow both, so that both web app and api will work together. Something like return [ /* …
13
votes
2 answers

What does "bindings" middleware do in Laravel 5.6?

Just as per the title. Default api middleware in Laravel 5.6 is listed in Kernel.php as: protected $middlewareGroups = [ 'api' => [ 'throttle:60,1', 'bindings', ], ]; I'd appreciate a layman's explanation of what bindings…
Inigo
  • 8,110
  • 18
  • 62
  • 110
1
2 3
37 38