Home > Backend Development > PHP Tutorial > How to use the corresponding interface of Laravel 5.5?

How to use the corresponding interface of Laravel 5.5?

php中世界最好的语言
Release: 2023-03-18 06:56:01
Original
1894 people have browsed it

We know that Laravel 5.5 is a very important version. A new return type has been added to the routing of Laravel 5.5: Responsable interface. Today I will bring you a case to introduce it in detail.

Look at the example:

use Illuminate\Contracts\Support\Responsable;
 
class ExampleObject implements Responsable
{
 public function construct($name = null)
 {
  $this->name = $name ?? 'Teapot';
 }
 
 public function status()
 {
  switch(strtolower($this->name)) {
   case 'teapot':
    return 418;
   default:
    return 200;
  }
 }
 
 public function toResponse()
 {
  return response(
   "Hello {$this->name}",
   $this->status(),
   ['X-Person' => $this->name]
  );
 }
}
Copy after login


When using this ExampleObject in routing, you can do this:

Route::get('/hello', function() {
 return new ExampleObject(request('name'));
});
Copy after login


In the Laravel Framework, the Route class can now check for this type (that implements the Responsable interface) when preparing the response content:

if ($response instanceof Responsable) {
 $response = $response->toResponse();
}
Copy after login


If you use multiple response types to organize your response content under the App\Http\Responses namespace, you can refer to the following example. This example demonstrates how to support Posts (a Collection of multiple instances):

posts = $posts;
 }
 
 public function toResponse()
 {
  return response()->json($this->transformPosts());
 }
 
 protected function transformPosts()
 {
  return $this->posts->map(function ($post) {
   return [
    'title' => $post->title,
    'description' => $post->description,
    'body' => $post->body,
    'published_date' => $post->published_at->toIso8601String(),
    'created' => $post->created_at->toIso8601String(),
   ];
  });
 }
}
Copy after login

The above is just a basic example to simulate a simple application scenario: return a JSON response, but you hope that the response layer is not simply implemented with built-in Instead of JSONizing the object, we need to do some content processing. The above examples also assume that the App\Http\Responses\Response class can provide some basic functions. Of course, the response layer can also contain some conversion code (similar to Fractal) instead of doing such conversion directly in the controller.

The controller code that cooperates with the PostIndexResponse class in the above example is similar to the following:


I believe you have mastered the method after reading these cases, more Please pay attention to other related articles on the php Chinese website!

Related reading:

Detailed explanation of binary search and quick sorting examples in python

php Binary search example code for recursive and non-recursive implementations

Detailed explanation of PHP binary search

The above is the detailed content of How to use the corresponding interface of Laravel 5.5?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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