What is the difference between App \ Http \ Requests and Request

4

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

    
asked by anonymous 26.05.2017 / 17:31

2 answers

4

The use Request will not work in Laravel, I think it would look 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 uasr use App\Http\Requests; you can call in your script just this:

new Request;

The use is 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 in Laravel:

<?php

use App\Http\Requests as Request;

You will be creating the Request alias 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 purpose of nicknames is the same of all programming languages that use namespace, is to create allow to probably type less and use two classes that have the same name, but different namespaces, so for example this would cause error because it would conflict:

<?php

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

But if you do this:

<?php

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

And you can use it like this:

$x = new Baz;
$y = new Baz2;
    
26.05.2017 / 17:34
0
  

What difference in Laravel, from using use App\Http\Requests to 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 your controllers .

You should be confusing with Illuminate\Http\Request , which is the class responsible for handling request in Laravel controllers.

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

But where does this Request come from?

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

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

This facade is called the same as Illuminate\Http\Request from the Service Container of Laravel.

To avoid making more than one trip between a facade and Service Container in>>, you may want to directly use Illuminate\Http\Request .

I do not particularly like the idea of aliases inside config/app.php . The code is dependent on a class that only exists virtually within the framework and can lead to this type of misunderstanding about which class to use.

    
29.05.2017 / 02:17