Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Performance different between various request methods in Laravel

on laravel, there is a specific class named Illuminate\Http\Request which provided an object-oriented way to interact with HTTP request which are send by client-side

it can be accessed by two methods as follows,

$name = $request->input('name');
$name = $request->name;

which one is the most efficient way to get the request?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The first one with input() is the mainly documented way of doing it, so I’d recommend using it as the functionnality is quite well established and the behavior is quite known.

The second one is a syntaxic sugar that map unexisting properties to inputs. It may be more concise however it uses a magic method call that will be potentially slower (check that a property exist, recognise it doesn’t, resolve the magic call and execute the magic call).

However, while the performance difference is quite negligible for most usages imho, I’ll not recommend using the second method anyway. As the Request class scemantics may change in future versions, you have no guarantee that a property will not be created with the same name as an input used in your application. This may or may not create bugs when upgrading the framework down the line as all your code will now be referencing this property instead of the inputed value as you intend it to.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading