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
12
votes
2 answers

Laravel: Encode JSON responses in UTF-8

I want to encode the JSON responses of my API to UTF-8, but every time I make a response I don't want to do this: return response()->json($res,200,['Content-type'=>'application/json;charset=utf-8'],JSON_UNESCAPED_UNICODE); So I thought about making…
JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61
11
votes
3 answers

How to retrieve a url parameter from request in Laravel 5?

I want to perform certain operations with a model in a middleware. Here is an example of what I want to achieve: public function handle($request, Closure $next) { $itemId = $request->param('item'); // <-- invalid code, serves for illustration…
Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
11
votes
2 answers

Restrict route access to non-admin users

Goal I'm trying to create Admin route restriction for my log-in users. I've tried a check to see if my user is log-in, and also if the user type is Admin, and if they are, I want to allow them access to the admin route, otherwise, respond a…
10
votes
1 answer

Laravel 5.3 Middleware class does not exist

I would like to make some authentication based on middleware.. But unfortunately it returns as the class is not exist Here is my middleware Staff.php
Yuko Pangestu
  • 193
  • 1
  • 2
  • 19
9
votes
3 answers

How to Solve Facade\Ignition\Http\Middleware\IgnitionEnabled?

----------------------------------------------------------------------------------+ | Domain | Method | URI | Name | Action | Middleware …
Joney Spark
  • 245
  • 2
  • 3
  • 13
9
votes
3 answers

Condition in laravel 5 Middleware always false

I'm trying to give condition in my Middleware. Here is my script if (auth()->check() && auth()->user()->type == 'TP001') { $menu->add("User Control",array('nickname' => "user",'class'=>'treeview')) ->append(' ') …
YVS1102
  • 2,658
  • 5
  • 34
  • 63
9
votes
9 answers

How to check user status while login in Laravel 5?

I have used Laravel Authentication (Quickstart). But I need to check the status of the user (approved/pending). If not approved, then an error will be shown in the login page. I need to know in which file I have to make the change and what is the…
Zaman
  • 117
  • 1
  • 2
  • 10
9
votes
1 answer

Modify input in laravel middleware

Some service makes HTTP request to my site and passes some input. This input has a little bit wrong structure for me, so I'm trying to modify it. I made a middleware and attached this middleware to my route. The handle method looks like…
Victor
  • 5,073
  • 15
  • 68
  • 120
8
votes
5 answers

Laravel CORS with Fruitcake

I make react project with laravel Back-end ... I have a CORS problem, I do everything like on link below, with fruitcake. Laravel 6 CORS policy issue with API but still not working. cors.php: 'paths' => ['api/*'], /* * Matches the…
Qli
  • 179
  • 2
  • 4
  • 19
8
votes
2 answers

Run Middleware Before Controller's Constructor On Laravel 5.1?

I have a middleware that authenticates a JWT user using tymon/jwt-auth package: public function handle($request, \Closure $next) { if (! $token = $this->auth->setRequest($request)->getToken()) { return $this->respond('tymon.jwt.absent',…
HTMHell
  • 5,761
  • 5
  • 37
  • 79
8
votes
1 answer

Using Auth::user in middleware

I'm trying to check if the URL entered is the same as the authenticated users slug in the database. So if a user goes to example.com/user/bob-smith and it is in fact Bob Smith logged in, the application will let Bob continue because his slug in the…
user1871245
7
votes
4 answers

laravel authorizeResource always denies access

I have created a resource controller for an API endpoint. I have also created a corresponding policy for the model. If I do a per method authorization check using $this->authorize('delete', $asset); then it works as expected. But if I add the…
JaChNo
  • 1,493
  • 9
  • 28
  • 56
7
votes
2 answers

How can I specify a guard in middleware for route?

I have two routes as follow: Route::GET('admins/', 'UserController@index')->middleware('jwt.auth'); Route::GET('visitors', 'UserController@indexVisitors')->middleware('jwt.auth'); And I have guards in auth.php: 'guards' => [ 'web' => [ …
Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110
7
votes
3 answers

Laravel grouping routes what is best prefix or middleware

When I start thinking grouping my routes and check the documentation. I lost there. There are too many things like prefix, middleware etc. What is the best way to group routes? Route::group(['middleware' => 'admin'], function ()…
Shahid Karimi
  • 4,096
  • 17
  • 62
  • 104
7
votes
2 answers

Redirect if authenticated in Laravel 5.3

I'm using the Auth scaffold in Laravel 5.3 and I've changed the routes for the auth. So instead of /login and /register I use /signin and /signup. In Laravel 5.2 we had this by default in the auth middleware, public function handle($request, Closure…
Ecaz
  • 111
  • 2
  • 12
1
2
3
37 38