Example analysis of basic usage of laravel 5.1 framework response

Time:2021-10-24

This article illustrates the basic usage of laravel 5.1 framework response. Share with you for your reference, as follows:

Just after recording the request in the last note, let’s talk about the response. Generally speaking, a request corresponds to a response. Users request us. We must make some logic and give feedback to others. This is the response.

1 basic response

We have used many responses in our previous notes, including strings:

Route::get('/', function () {
  Return 'welcome';
});

Note: what we write is to return a simple string, but the laravel framework will automatically form it into a response.

1.1 custom response header


Route::get('/', function () {
  return (new \Illuminate\Http\Response('hello', 200))->header('Content-Type', 'text/html');
});

We can also use the response global help function to generate corresponding. If you want to specify multiple response headers, you can chain operation ↓


Route::get('/', function () {
  return response('hello')
    ->header('Content-Type', 'text/html')
    ->header('something', 'something');
});

1.2 adding cookie information

We can use the withcookie function to add cookie information.


Route::get('/', function () {
  return response('hello')
    ->header('Content-Type', 'text/html')
    ->withCookie('cookie-name', 'value');
});

1.3 return to view

Before, we used the help function view (). Today, let’s have some trouble –:


Route::get('/', function () {
  return response()
    ->view('welcome')
    ->withCookie('newCookie', 'home');
});

1.4 return JSON

When we develop APIs, JSON must be able to:


Route::get('/', function () {
  return response()
    ->json([
      "name" => "k",
      "age" => 24
    ]);
});

1.5 redirection

We have used redirection before. Let’s review:


Route::get('/', function () {
  return redirect()->action('Admin\[email protected]');
});

Sometimes, we need to redirect to the previous position of the current page. You can use the back function:

Route::get('/', function () {
  //WithInput can return the information entered by the user on the previous page, so that the user does not have to input repeatedly.
  return back()->withInput();
});

When the user inputs correctly, the redirection should give some prompts to others, right? We can use a one-time session to pass:


Route::get('/', function () {
  return redirect()->action('Admin\[email protected]')->with('status', 'Success');
});
  @if(session('status'))
    <div>
      {{ session('status') }}
    </div>
  @endif

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.