Laravel
196
w3alert.com
06-01-2020
The HTTP Request Communicate Between routes and Controller in Laravel. The Illuminate \ Http \ Request example provides several ways to check HTTP requests on your web application.
If you want to access the request object in your web application controller. You need to place the Illuminate\ Http\Request class on your controller constructor or method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class MyController extends Controller { /** * Store a new user. * * @param Request $request * @return Response */ public function store(Request $request) { $email = $request->input('email'); // } }
If you want to access the request object in your web application controller with route parameters. You also need to place the Illuminate\ Http\Request class on your controller constructor or method.
You get your route parameter id by defining your controller method like the following:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class MyController extends Controller { /** * Update the specified user. * * @param Request $request * @param int $id * @return Response */ public function update(Request $request, $id) { //laravel access request with parameters } }
If you want to access the request object in your web application routes. You also need to place the Illuminate\ Http\Request class on your routes.
You get or access request object in your web application route, like the following:
use Illuminate\Http\Request; Route::get('/post', function (Request $request) { //access request object });
To get/retrieve request URI/URL in your controller, you can use request->path() method for that. You could do like below:
$uri = $request->path();
The path method returns the request's URL/URI. So, if your request incoming from http://w3alert.com/laravel/tutorial, this method will return laravel/tutorial:
To get the full request URL, you may use the URL method on the request instance:
$url = $request->url();
To get/retrieve the request method, you can use request->method() method for that. You could do like below:
$reqMethod = $request->method(); dd($reqMethod);
To get/retrieve the inputs from HTTP request, you can use $request->input(), $request->input_parameter_name, $reqest->get(), $request->all() method for that. You could do like below:
//get single inputs $email = $request->input('email'); $email = $request->email; //get all inputs $all = $request->all(); $all = $request->get();
To get the cookies to value from the request, you can use $request->cookies() method for that. You could do like below:
$value = $request->cookie('name');
To Get/retrieve File from HTTP request, you can use $request->cookies() method for that. You could do like below:
$file = $request->file('image');
You can also check if a file exists on the HTTP request or not. You can use the hasFile method:
if ($request->hasFile('image')) { // }
To save/store file in folder, you can use the laravel move method for that. Basically this method will store the file from the folder. You could like below:
$request->file('image')->move($destinationPath); $request->file('image')->move($destinationPath, $fileName);
This tutorial idea has taken from this https://laravel.com/docs/7.x/requests