What’s the difference between Http Requests and Request App

Asked

Viewed 249 times

4

What difference in Laravel, to use use App\Http\Requests for use Request?

  • If you must be talking about FormRequest inside the namespace App\Http\Requests that are created in your application (by the command php artisan make:request) and of Request that is of namespace Illuminate\Http\Request which are related, only that the first has the validation factor embedded in the code.

2 answers

4

The use Request it won’t work in the Laravel, I think it would be something like:

use App\Http\Requests as Request

If the name is App\Http\Requests, if the name is App\Http\Request (without s) just wow use App\Http\Requests; that can call in your script only this:

new Request;

The use not to import libraries or classes but to create nicknames for the namespaces, as I explained in:

So for a little more detail, if you do this specifically on the Laravel:

<?php

use App\Http\Requests as Request;

You will be creating the nickname Request for the namespace and class App\Http\Requests

If you do this:

<?php

use App\Http\Requests;

You will be creating the nickname Requests (with s) for the namespace and class App\Http\Requests, the goal of nicknames is the same of all programming languages that use namespace, is to create allow you to probably type less and use two classes that have the same name, but different namespaces, so for example this would cause error as it would conflict:

<?php

use Foo\Bar\Baz;
use Foo2\Bar2\Baz;

But if I do this:

<?php

use Foo\Bar\Baz;
use Foo2\Bar2\Baz as Baz2;

And you can wear it like this:

$x = new Baz;
$y = new Baz2;
  • I saw in some questions that there is a difference, but unfortunately I get confused, follow the link: https://laracasts.com/discuss/channels/laravel/use-apphttprequests-or-illuminatehttprequest-which-should-it-be

  • @Biabaldasso I will read about Laravel, I only used his basics until today, but as Virgilio specifically in Laravel there are details of "architecture", I will adjust the response and warning you ;)

  • Right, because the use Request works yes in Laravel.... That’s why the doubt...

0

What difference in Laravel, to use use App\Http\Requests for use Request

There is no class called App\Http\Requests. This is a namespace created within your application when using the command php artisan make:request and with it you can create Form Requests to pre-validate the requests that will reach their controllers.

You must be confused with Illuminate\Http\Request, which is the class responsible for manipulating the request us controllers of Laravel.

The difference between Illuminate\Http\Request and the Request is none :)

But where does that come from Request?

Laravel creates an alias for some stabbing within the config/app.php

'aliases' => [
    'Request' => Illuminate\Support\Facades\Request::class,
],

That one stabbing for his made flame the same Illuminate\Http\Request from the Service Container of Laravel.

To avoid making more than one trip between a stabbing and the Service Container, prefer to use directly the Illuminate\Http\Request.

I particularly dislike the idea of aliases within the config/app.php. The code is dependent on a class that exists virtually only within the framework and may lead to this kind of misunderstanding about which class to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.