Home  >  Article  >  PHP Framework  >  How to perform requests in laravel

How to perform requests in laravel

不言
不言Original
2018-12-27 10:48:593739browse

How to execute request request in How to perform requests in laravel? This article will introduce you to the method of executing requests in How to perform requests in laravel. Friends who need it can refer to it. I hope it will be helpful to you.

How to perform requests in laravel

Let’s first take a look at what request is?

The client (such as a web browser) and the server (web server) communicate using the HTTP protocol. The client sends a request (request) to the server, and the server responds to the request and returns a response (response). HTTP protocol There are several ways to perform a request, the most common being the POST and GET methods. An HTTP request consists of two parts: a header containing data about the request and a body containing the data to be processed by the server.

The following describes how to pass the data to be processed by the server.

How to pass values ​​in POST/GET in Laravel

The request can include data to be processed by the server.

For example, if your username is username=John.

Requests can be sent from HTML forms.

<form method="POST(或者GET)" … >
    …
    <input type="text" name="username" value="" … >
    …
</form>

Specify the data name in the name attribute using an element such as input. The user's input value is set in the value attribute.

When you submit this form, every data is included in the request.

For the POST method, the body of the request is as follows.

其他的数据&username=John&其他的数据

For the GET method, the query string contained in the URL is as follows:

http://localhost:8000/users?其他的数据&username=John&其他的数据

In addition, the GET method request is usually sent from an HTML link.

You can specify the URL in the href attribute of the a element but include the data in the query string.

In the controller action, we get the data from the Laravel request instance.

You can receive a request instance as a parameter of the operation, or you can obtain it by calling the helper function request().

You can obtain data from the instance in the following ways.

$request->all(); // 获取所有数据作为关联数组

$request->only([ &#39;username&#39;, &#39;password&#39;]); // 仅将某些数据作为关联数组
$request->except([&#39;credit_card&#39;]); // 只获取一些数据(非指定数据)作为关联数组

$request->input(&#39;username&#39;); // 通过input取得个别的数据
$request->username; // 通过动态属性获取个别数据
request(&#39;username&#39;); // 使用辅助函数request()获取个别数据

Making a request to Laravel

As an example, let us use the GET method to request multiple data.

将以下路由添加到routes / web.php文件中。
Route::get(&#39;/users&#39;, function () {
    $request = request(); // 取得请求实例
    $data = $request->all(); //获取所有数据作为关联数组
    return $data; // 返回获得的关联数组(转换为JSON)
});

Access http://localhost:8000/users?first_name=John&last_name=Do

from the browser. The JSON is displayed as follows, which can confirm that you have received the data.

{"first_name":"John","last_name":"Doe"}

The above is the detailed content of How to perform requests in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn