Home > Article > Backend Development > How to implement Laravel 5.5 reactive interface
Laravel is a simple and elegant PHP Web development framework (PHP Web Framework). It can free you from messy codes like noodles; it can help you build a perfect network APP, and every line of code can be concise and expressive.
Laravel 5.5 adds a new return type to routing: Responsable interface ). This interface allows objects to be automatically converted into a standard HTTP response interface when returned from a controller or closure route. Any object that implements the Responsable interface must implement a toResponse() method, which converts the object into an HTTP response object.
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] ); } }
When using this ExampleObject in routing, you can do this:
Route::get('/hello', function() { return new ExampleObject(request('name')); });
In the Laravel framework, the Route class can now prepare response content Check this type (implementing the Responsable interface):
if ($response instanceof Responsable) { $response = $response->toResponse(); }
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 sample demonstrates how to support Posts (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(), ]; }); } }
The above is just a basic example to simulate a simple application scenario: return a JSON response, but you hope that the response layer does not simply use the built-in implementation to convert the object to JSON ization, but to do some content processing. The above example also assumes App\Http\Responses\Response This 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:
If you want to know more details about this interface, you can view the commit of the relevant code in the project.
The above content is a detailed explanation of the responsive interface provided in Laravel 5.5 for responding to requests. I hope it can help everyone.
Related recommendations:
Laravel5.5 new features error reporting and graphic introduction of the display
About friendly error reporting in Laravel5.5 Display and detailed explanation
Detailed introduction of Package Auto Discovery in Laravel5.5
The above is the detailed content of How to implement Laravel 5.5 reactive interface. For more information, please follow other related articles on the PHP Chinese website!