How to implement the Laravel 5.5 responsive interface

Source: Internet
Author: User
Tags php web development
Laravel is a simple, elegant PHP Web development Framework (PHP Web framework). It frees you from the messy code of noodles, and it helps you build a perfect web app, and every line of code can be concise and expressive.

A new return type is added to the Laravel 5.5 route: the interface (responsable) is appropriate. This interface allows an object to be automatically converted to a standard HTTP response interface when returned from a controller or a closure route. Any object that implements the Responsable interface must implement a method named Toresponse () that transforms the object into an HTTP response object.

See 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;}  }   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 examine this type of (implementing the Responsable interface) when preparing the response content:

if ($response instanceof responsable) {$response = $response->toresponse ();}

If you are using multiple response types to organize your responses under the App\http\responses namespace, you can refer to the following example. This example demonstrates how to support Posts (a Collection consisting 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 ' and $post->body,    ' Published_date ' = $post->published_at->toiso8601string (),    ' created ' + $post->created_at-> Toiso8601string (),   ];  }); }}

The above is just a basic example of simulating a simple scenario: returning a JSON response, but you want the response layer not to simply json the object with the built-in implementation, but rather to do some content processing. The above example also assumes that the App\http\responses\response class can provide some basic functionality. Of course, the response layer can also contain some conversion code (similar to Fractal), rather than directly in the controller to do such a conversion.

The controller code that collaborates with the Postindexresponse class in the example above resembles the following:

If you want to learn more about this interface, you can review the commit of the relevant code in the project.

The above content is Laravel 5.5 in response to the request to provide a response to the interface detailed, I hope to help everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.