Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the current request in controller

In other MVC frameworks, accessing to the current request object is as simple as $this->request. However in the Laravel, I generally see that Request $request is generally injected to each action (public function edit($id, Request $request)). It seems like a boilerplate. Is there any better way to access the request? (I now that I can use inheritance to use $this->request, I am looking for the Laravel way to do that.)

update:

I found out using app('request') I can access to the current request. However, I am not sure of its potential pros and cons.

like image 797
Handsome Nerd Avatar asked Aug 09 '15 01:08

Handsome Nerd


People also ask

How can you retrieve the full URL for the incoming request?

Retrieving the Request URI The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.

What is request () in laravel?

Introduction. Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.

What method on a request instance provides access to an uploaded file called Foo?

laravel request has file regarding path method declares as well as returns the request's path erudition. The path method will return foo/bar: if the incoming request is targeted at http://domain.com/foo/bar.

Which method allows you to verify that the incoming request path matches a given pattern?

rs. Path annotation in JAX-RS is used to define a URI matching pattern for incoming HTTP requests. It can be placed upon a class or on one or more Java methods.


1 Answers

In Laravel 5, you can use the request() helper:

// to get the current request object
$request = request();

// or to just get a value from the request
$value = request("field", "default");

See https://laravel.com/docs/5.6/helpers#method-request

like image 186
miken32 Avatar answered Sep 24 '22 01:09

miken32