Home > PHP Framework > Laravel > How to get the current method in laravel template

How to get the current method in laravel template

PHPz
Release: 2023-05-26 16:10:37
Original
667 people have browsed it

In Laravel, you can use the method() method of the IlluminateHttpRequest class to get the current method. This method will return the HTTP method of the current request.

In Laravel's Blade template, you can use {{ request()->method() }} to get the method of the current request. For example:

@if (request()->method() == 'POST')
    请求方式为 POST
@elseif (request()->method() == 'GET')
    请求方式为 GET
@else
    请求方式为其它
@endif
Copy after login

In the controller, you can use $request->method() or $request->getMethod() to get the method of the current request. For example:

public function index(Request $request)
{
    if ($request->method() == 'POST') {
        // 处理 POST 请求
    }

    if ($request->getMethod() == 'GET') {
        // 处理 GET 请求
    }
}
Copy after login

In addition, in the controller, you can also use PHP's built-in $_SERVER['REQUEST_METHOD'] variable to obtain the method of the current request. For example:

public function index()
{
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        // 处理 POST 请求
    }

    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        // 处理 GET 请求
    }
}
Copy after login

It should be noted that the way of using the $_SERVER['REQUEST_METHOD'] variable is not as elegant and convenient as the way of using the Request class provided by Laravel.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template