Is it desirable to associate application data with a request in Laravel 5

Would it be advisable if I authenticate with middleware and add some data to the \Illuminate\Http\Request $request object and use that data in the controller by entering \Illuminate\Http\Request $request in the controller method?

The reason is that the application must make a database call to find out if the credentials are valid and if they are, it returns something like the primary key, which I use in subsequent db operations.

At the moment, everything is done in the controller. If I used separate middleware for authentication, can I bind the data that my controller should use to the request object if the middleware check passes? if so, how should I do this?

Inspiration - Expressjs way to bind and transmit data on demand through a stack of intermediate lines / routes.

+10
php middleware laravel
Apr 13 '15 at 12:09
source share
3 answers

I donโ€™t understand - why donโ€™t you just use Laravel authenticator?

You speak:

The reason is that the application must make a database call to find out if the credentials are valid and if they are, it returns something like the primary key, which I use in subsequent db operations.

And is that exactly what 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 using an outdated authentication system. In your middleware, you can do something like this:

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

This means that you can perform your check using the neo4j db graph - and if it returns true that the user will be authenticated correctly, you yourself will write them to the Laravel system.

+6
May 11 '15 at 7:16
source share

Yes, this is probably a good way to do this, since the Laravel built-in authentication system works the same way: you can access the registered user through $request::user() . See http://laravel.com/docs/5.0/authentication#retrieving-the-authenticated-user

+2
Apr 13 '15 at 12:15
source share

It is good to verify authentication in middleware. In my application, we use the same functions to check if the user sends the correct access_code to access the API methods. Even Laravel itself handles secure routes using Authenticate middleware.

The problem is that there is no silver bullet in how and where to store additional data.

One way is to save this in a user session.

Secondly, you need to use the Illuminate\Foundation\Application class. You can enter it in your __constructor() middleware and use it to save your data. Application class extends Container class that implements the ArrayAccess interface, which allows you to access its properties, such as an array. This allows you to not only get variables from the application, but also store them. Not the best way than the easiest.

 public function __construct(\Illuminate\Foundation\Application $app) { $app['_foo'] = 'bar'; } 

There are more of these hacks, but they are the simplest.

+1
May 12, '15 at 10:03
source share



All Articles