Skip to content

2_3_Requeust and response flow

williamyeh edited this page Dec 6, 2016 · 4 revisions

The following figure illustrates how VMS process client's request and response to client.

architecture flow

The client (view) sends a request in JSON format to VMS, according to HTTP method and URL which are defined in app/Http/routes.php, HTTP Routing routes the request to a corresponding method in a controller.

If any HTTP Middleware is registered on a routing, the middleware will be invoked to filter request. For example, there is a middleware \Tymon\JWTAuth\Middleware\GetUserFromToken for checking the authentication token in a request.

Next, before entering a controller, if there is a HTTP request dependency injection on a controller, the request will be created and called. Since the request may contain request validation logic and authorization control, it avoids fat controllers and duplicated code in controllers.

A request implements authorize()1 and rules()2 methods. The authorize() calls Policies via Gate and returns true or false for determining whether the request is authorized or not. The rules() returns the validation rules 3 in array type.

After that, a controller retrieves input data from requests and manipulate Models for work with database. If the model data should be sent as a response to the client, it will be passed to Transformers in order to transform from object to array as JSON response.

Reference

  1. authorize(): https://laravel.com/docs/5.2/authorization#within-form-requests
  2. rules(): https://laravel.com/docs/5.2/validation#form-request-validation
  3. validation rules: https://laravel.com/docs/5.2/validation#available-validation-rules