1. Get Request instance
In Laravel, the Request instance of the current Request is generally obtained through the controller method dependency injection.
We define an implicit controller for testing in this section. First, we define a route in routes. php as follows:
Route: controller ('request', 'requestcontroller ');
Then we create a controller RequestController. php under app/Http/Controllers:
<? Php
Namespace App \ Http \ Controllers;
Use Illuminate \ Http \ Request;
Use Illuminate \ Http \ Response;
Use App \ Http \ Requests;
Use App \ Http \ Controllers \ Controller;
Class RequestController extends Controller
{
Public function getBasetest (Request $ request)
{
$ Input = $ request-> input ('test ');
Echo $ input;
}
}
To access the getBasetest method, we only need to access the http://laravel.app in the browser: 8000/request/basetest? Test = laravelacademy, so the page will output:
Laravelacademy
2. Obtain the request URL and method
2.1 obtain the request URL
To obtain the URL of the current Request, we can use the path method on the Request instance. Note that this method returns the relative Request path. If you want to obtain the absolute Request path, you can use another method on the Request instance -- url:
Public function getUrl (Request $ request)
{
// Match the request/* URL to continue the access
If (! $ Request-> is ('request /*')){
Abort (404 );
}
$ Uri = $ request-> path ();
$ Url = $ request-> url ();
Echo $ uri;
Echo '<br> ';
Echo $ url;
}
We access http://laravel.app: 8000/request/url in a browser and enter the following content on the page:
Request/url
Http://laravel.app: 8000/request/url
2.2 Request Method
We can also call the getMethod method on the Request instance to obtain the method of the current Request:
Public function getMethod (Request $ request ){
// Non-get requests cannot be accessed
If (! $ Request-> isMethod ('get ')){
Abort (404 );
}
$ Method = $ request-> method ();
Echo $ method;
}
3. GET request data
3.1 Current request input
You can use the input method on the Request instance to obtain the Request input data. This method can receive two parameters. The first parameter is the parameter name, and the second parameter is the default value returned if the parameter name is null. In addition, this method also supports obtaining the value of the array parameter, the test method is defined as follows:
Public function getInputData (Request $ request ){
// Obtain the name parameter passed by the GET method. The default value is LaravelAcademy.
$ Name = $ request-> input ('name', 'laravelemy Emy ');
Echo $ name;
Echo '<br> ';
Echo $ request-> input ('test. 0. Name ');
}
Enter http://laravel.app: 8000/request/input-data in the browser? Name = Laravel & test [] [name] = Academy, the page output is as follows:
Laravel
Academy
If you want to determine whether the input parameter name exists before obtaining the input value, you can use the has Method. For example, to determine whether the input parameter contains hello, you can use the following method:
If ($ request-> has ('Hello '))
Echo $ request-> input ('Hello ');
To obtain all input parameter values, you can use the all method on the Request instance. To obtain some input values, you can use the only method. To exclude some input parameter values, you can use the distinct t method:
Public function getInputData (Request $ request ){
$ AllData = $ request-> all ();
$ OnlyData = $ request-> only ('name', 'Hello ');
$ Response tdata = $ request-> response t ('Hello ');
Echo '<pre> ';
Print_r ($ allData );
Print_r ($ onlyData );
Print_r ($ response tdata );
}
Access http://laravel.app in a browser: 8000/request/input-data? Name = Laravel & test [] [name] = Academy & hello = World. The page output is as follows:
Array
(
[Name] => Laravel
[Test] => Array
(
[0] => Array
(
[Name] => Academy
)
)
[Hello] => World
)
Array
(
[Name] => Laravel
[Hello] => World
)
Array
(
[Name] => Laravel
[Test] => Array
(
[0] => Array
(
[Name] => Academy
)
)
)
3.2 Last Request input
The above methods are used to obtain the input of the current request. If you want to obtain the input of the previous request, when processing the last Request, you must use the flash method on the Request instance to temporarily save the Request data to the session, then, in the current Request, use the old method on the Request instance to obtain the data stored in the session. After the data is obtained, the Request data stored in the session will be destroyed:
Public function getLastRequest (Request $ request ){
$ Request-> flash ();
}
Public function getCurrentRequest (Request $ request ){
$ LastRequestData = $ request-> old ();
Echo '<pre> ';
Print_r ($ lastRequestData );
}
If we want to redirect to the current request URL after the data is saved in the previous request, we can use the following method to define the getLastRequest method:
Public function getLastRequest (Request $ request ){
// $ Request-> flash ();
Return redirect ('/request/current-request')-> withInput ();
}
So we access the http://laravel.app in the browser: 8000/request/last-request? Name = test & passwd = 123456, the page will be redirected to http://laravel.app: 8000/request/current-request and output the following content:
Array
(
[Name] => test
[Passwd] = & gt; 123456
)
Refresh the page again and the output is blank:
Array
(
)
This indicates that the request data in the session is cleared after the data is retrieved. For more methods, see the official HTTP request documentation.
4. Obtain Cookie data
We can use the cookie method on the Request instance to obtain cookie data. This method can receive a cookie value returned by a parameter name. If no parameter is input, all cookie values are returned by default:
Public function getCookie (Request $ request ){
$ Cookies = $ request-> cookie ();
Dd ($ cookies );
}
We access http://laravel.app: 8000/request/cookie in the browser, page output:
Array: 2 [
"XSRF-TOKEN" => "fsp1erkcxnxx0wcy1_wjur3ruh8c09vzxnr64nbc"
"Laravel_session" => "820e88a52c45f8dbda55e8c6aaaa9bbca2c760ef"
]
We can call the withCookie method on the Response instance to add a cookie:
Public function getAddCookie (){
$ Response = new Response ();
// The first parameter is the cookie name, the second parameter is the cookie value, and the third parameter is the validity period (minutes)
$ Response-> withCookie (cookie ('Website', 'laravelemy Emy. org ', 1 ));
// If you want to use cookies for a long time, use the following methods:
// $ Response-> withCookie (cookie ()-> forever ('name', 'value '));
Return $ response;
}
The getCookie method is redefined as follows:
Public function getCookie (Request $ request ){
$ Cookie = $ request-> cookie ('Website ');
Echo $ cookie;
}
Next we access the http://laravel.app: 8000/request/add-cookie in the browser, then the http://laravel.app: 8000/request/cookie, the page output is as follows:
LaravelAcademy.org
5. Upload files
We define the file upload page and upload process as follows:
// File upload form
Public function getFileupload ()
{
$ PostUrl = '/request/fileupload ';
$ Csrf_field = csrf_field ();
$ Html = <CREATE
<Form action = "$ postUrl" method = "POST" enctype = "multipart/form-data">
$ Csrf_field
<Input type = "file" name = "file"> <br/>
<Input type = "submit" value = "submit"/>
</Form>
CREATE;
Return $ html;
}
// File Upload processing
Public function postFileupload (Request $ request ){
// Determine whether the request contains an uploaded file with name = file
If (! $ Request-> hasFile ('file ')){
Exit ('upload file is empty! ');
}
$ File = $ request-> file ('file ');
// Determine whether an error occurs during file Upload
If (! $ File-> isValid ()){
Exit ('file Upload error! ');
}
$ DestPath = realpath (public_path ('images '));
If (! File_exists ($ destPath ))
Mkdir ($ destPath, 0755, true );
$ Filename = $ file-> getClientOriginalName ();
If (! $ File-> move ($ destPath, $ filename )){
Exit ('failed to save the file! ');
}
Exit ('file uploaded successfully! ');
}
The code above shows that we can use the file method on the Request instance to obtain the Upload file instance. The parameter received by this method is the name attribute of the Upload file input tag, this File Upload instance is a Symfony \ Component \ HttpFoundation \ File \ UploadedFile class instance. For more information about the available methods of this instance, see UploadedFile API documentation.