Laravel HTTP(HyperText Transfer Protocol) request method is the way to structure the request and response to set the communication of Client and server or its a protocol to transfer the data over the web. Http follow the server-client Model. In Laravel HTTP request get processed through routes/web.php by default.
We can access the Http Request by declearing the Illuminate\Http\Request in Controller.
public function personalData(Request $request) { $name = $request->input('name');
}
HTTP Request Path and Methods
Illuminate\Http\Request instance provides various method to access the Http request and its parameter which is trying to access the application.
path() method will return the information about the url path. For example: Our URL is https://www.abcd.com/my-profile/umesh, if we use path() method to get the request it will return /my-profile/umesh.
For Example :
We can fetch the entire url itself using url() method and fullurl() method. ce url method will return the url without string whereas fullurl method will return with string. Below is the code for your reference.
path() method will return the information about the url path. For example: Our URL is https://www.abcd.com/my-profile/umesh, if we use path() method to get the request it will return /my-profile/umesh.
For Example :
$uriPath = $request->path();
We can fetch the entire url itself using url() method and fullurl() method. ce url method will return the url without string whereas fullurl method will return with string. Below is the code for your reference.
$urlWthtString = $request->url(); // return url without String
$urlWthString = $request->fullUrl(); // return url with String
Fetching the Request Method
We can fetch the HTTP request type using method() function as well as we can check whether the requested method is satisfying the type using ismethod() function .
$methodType = $request->method();
// This Will give you the request method type whether it is post, get etc.. if ($request->isMethod('post')) { //
}
Laravel Retrieving Input From HTTP Request
We can retrieve all the input data as a array which is coming through the HTTP Request using all() method.
$inputData = $request->all();
Illuminate\Http\Request instance We can get the request data using input method. we can pass the second argument in input method. If input method don't have the value it will take second argument as default value. See the code below for reference.
$firstName = $request->input('firstName'); $firstName = $request->input('firstName', 'Umesh');
We can use input() method without having any argument to get the input value as a associative array.
$input = $request->input(); // this return the input as a associative array.
Checking Input Value is Present or not
⇛ We can check whether the input value is there or not, we use has() method. has() method will return the value as true or false.
if ($request->has('firstName')){ // $request->has('firstName') will return the value as true or false.
}
⇛ has() method will check the array input also. check the code below for reference.if ($request->has(['firstName', 'emailId'])){ // If both array input will be there, It will return true or false.
}⇛ hasAny() method will check if any input will be there it will return true. Below is the code for your reference.
if ($request->hasAny(['firstName', 'emailId'])){ // if any Input will be there it will return true
}
Redirecting and Flashing the input
return redirect('personal-form/save')->withInput(); return redirect('personal-form/save')->withInput($request->except('userpass'));
Handling File Upload using HTTP Request
Sometime we need to access the uploaded file using HTTP request using file() method. The Illuminate\Http\UploadedFile class have file() method which returns an instance. We can check using hasFile() method that HTTP request have the file or not. Below is the code for your reference.
$fileImage = $request->file('candidate_photo');
$fileImage = $request->candidate_photo;if ($request->hasFile('candidate_photo')){ // }
Uploaded File Path and Extension
UploadFile class provide some predefined method through which we can access the file path and its Extension.
$Photopath = $request->candidate_photo->path();
$photoextension = $request->candidate_photo->extension();