Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it advisable to bind application data to request in Laravel 5

Would it be advisable, if i am doing authentication in a middleware and adding some data to the \Illuminate\Http\Request $request object and using that data in the controller by injecting \Illuminate\Http\Request $request into the controller method?

The reason, the app needs to make a database call to find out if the credentials are valid and if it is, it returns something like a primary key that i use in subsequent db operations.

At the moment, everything is done in the controller. If i were to use a separate middleware for authentication, can i bind the data that my controller needs to the request object if the middleware check passes? if so, how should i go about doing it?

Inspiration - Expressjs way of binding and passing data along the request through a stack of middlewares / routes.

like image 621
Swaraj Giri Avatar asked Apr 13 '15 12:04

Swaraj Giri


People also ask

Which method allows you to verify that the incoming request path matches a given pattern in Laravel?

The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method.

How does request work in Laravel?

Laravel itself creates an instance of the application, is the initial/first step. Next step will occur on the Kernel part of the application. The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application .

What is illuminate HTTP request in Laravel?

Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.

Does Laravel have request?

The has MethodThe $request->has() method will now return true even if the input value is an empty string or null . A new $request->filled() method has been added that provides the previous behaviour of the has() method. The $request->exists() method still works, it is just an alias for $request->has() .


2 Answers

I dont understand - why dont you just use the Laravel authenticator?

You say:

The reason, the app needs to make a database call to find out if the credentials are valid and if it is, it returns something like a primary key that i use in subsequent db operations.

And that is exactly what the Laravel Authenticator does?

Then in your controller you can just do

`auth()->user()` // gives you the user record
`auth()->id()` // user_id from the DB
`auth()->user()->name` // gives you the `name` column off the record. You can change it to anything.

Edit: Meanwhile - you can still use the Laravel Authenticator package whilst using a legacy system for authentication. In your middleware you can do something like this:

if (doLegacyCheckHere()) {
      Auth::loginUsingId(1);
}

This means you can do your check via the neo4j graph db - and if it returns true that the user is authenticated correctly - then you just log them into the Laravel system yourself.

like image 151
Laurence Avatar answered Sep 29 '22 12:09

Laurence


Yes, that is probably a good way to do it, as the build-in Laravel Authentication system works the same way: you can access a logged in user via $request::user(). See http://laravel.com/docs/5.0/authentication#retrieving-the-authenticated-user

like image 38
Jeroen Noten Avatar answered Sep 29 '22 12:09

Jeroen Noten