1

my question is why is laravel allow to just use use Auth; and don't have to write the whole directory but when I use other class like Request I have to write all directory like use Illuminate\Http\Request; and not able to just write use Request;

  • Does this answer your question? [PHP namespaces and "use"](https://stackoverflow.com/questions/10542012/php-namespaces-and-use) – Mike Doe May 22 '21 at 05:28

1 Answers1

1

A quick note — in this context, Illuminate\Http\Request is a {namespace}\{class name}, not a directory.

The Request Facade

First, there absolutely is a Request facade. I can quickly demonstrate some basic functionality.

use Request;

Request::merge(['foo' => 'bar']);

dd(Request::all());
array:1 [▼
  "foo" => "bar"
]

You can achieve the same result using the request() helper, without the need to explicitly import the facade.

Automatic Dependency Injection

I suspect that your question may actually be referring to Laravel's automatic dependency injection. In this case, Laravel has already resolved and populated an Illuminate\Http\Request instance behind the scenes. It will only ->bind() this instance to the method's $request parameter if it sees the correct type-hinting. This is why you are unable to simply import the facade.

Route::get('sample/request', 'SampleController@update');
use Illuminate\Http\Request;

class SampleController
{
    public function update(Request $request)
    {
        dd([
            'class_name' => get_class($request),
            'params' => $request->all()
        ]);
    }
}

.../sample/request?foo=bar

array:2 [▼
  "class_name" => "Illuminate\Http\Request"
  "params" => array:1 [▼
    "foo" => "bar"
  ]
]
matticustard
  • 4,850
  • 1
  • 13
  • 18