Analysis of common usage examples of request operation in laravel 5.1 framework

Time:2021-10-25

This article describes the common usage of request operation in larave5.1 framework. Share with you for your reference, as follows:

This article mainly talks about request, which is used whenever users visit our website functions. We can use some data in request to make appropriate response.

1 common attributes and methods

We can get some important attributes from the request for some logical processing. The following are the commonly used attributes in the request:

1.1 get user input

(the premise is to create a restful controller and declare the route). The request can obtain the data entered by the user. Let’s take a look

1.1.1 input

public function index(Request $request)
  {
    //First, we inject the request dependency into the index method.
    //Gets the name parameter entered by the user.
    dd($request->input('name'));
  }

In this way, you can enter in the browserlocalhost/admin?name=kIt will print out K.

If we can’t get the name parameter, we can pass in the second parameter to the input method, which means that if we can’t get the name parameter, we use the value of the second parameter.

1.1.2 all() get all


  public function index(Request $request)
  {
    dd($request->all());
  }

The all method returns an array in which all user input data can be obtained.

1.1.3 only and except


  public function index(Request $request)
  {
    dd($request->only('name'));
  }
  public function index(Request $request)
  {
    dd($request->except('name'));
  }

Only means to get only some parameters, except means to get other parameters without getting some parameters.

1.1.4 has method


  public function index(Request $request)
  {
    dd($request->has('age'));
  }

The has method returns a bool value. When the parameter exists, it returns true. If it does not exist, it returns false.

1.2 get URL


dd($request->url());

The returned URL does not take a get parameter, that is:localhost8000/admin?name=kreturnlocalhost8000/admin


dd($request->fullUrl());

Fullurl returns all addresses, that is:localhost8000/admin?name=kreturnlocalhost8000/admin?name=k

1.3 acquisition request method

public function index(Request $request)
  {
    if ($request->method() == 'GET'){
      Return 'get method';
    }
    return 'other';
  }

There’s nothing to say about this. Get is get, post is post

1.4 judgment path

if ($request->is('admin/*')){
    Return 'background';
  }

Mainly judge that the URL path * can be used as a wildcard.

1.5 get URI


dd($request->path());

Input:http://localhost:8000/admin?name=adle&age=14Get admin

For more information about laravel, readers who are interested can see the special topics on this site: introduction and advanced tutorial of laravel framework, summary of PHP excellent development framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation, and summary of common database operation skills for PHP

I hope this article will be helpful to your PHP Programming Based on laravel framework.