Get Started. It's Free
or sign up with your email address
Rocket clouds
Request and Response by Mind Map: Request and Response

1. Laravel's request Lifecycle

1.1. Request coming to app via Middleware

1.2. Middleware decorate the request

1.3. Application resolve the request and makes response

1.4. Response is decorated (in reverse order comparing to request-decorated)

1.5. User receive response

2. Bootstrapping the Application

2.1. The flow of bootstrapping as above

2.2. kernel is core router of every Laravel Application

2.2.1. Web kernel

2.2.2. Console kernel (for console commands)

2.2.3. has `handle()` methods for taking in Illuminate Request and returning Illuminate Response

2.3. Service Provider

2.3.1. class that encapsulates logic that application need to run

2.3.2. ServiceProvider have two important methods: `boot` and `register`

2.3.2.1. register: bind classes and aliases to container

2.3.2.2. boot: binding event listener, defining routes

2.3.3. defer a service providers to stop bootstrapping unless the bindings is explicitly requested from container

3. Request Object

3.1. extension from Symfony's HttpFoundation\Request

3.2. getting a Raw request: $request = Illuminate\Http\Request::capture()

3.3. Getting Laravel Request

3.3.1. type-hinting: ``` public function index(Request $request){ } ```

3.3.2. using global helper: $request = request()->all()

3.3.3. using app() global methods . app(Illuminate\Http\Request::class); . app('request');

3.4. Get Basic Information about a Request

3.4.1. Basic User input

3.4.1.1. all(): return array of all user-inputted

3.4.1.2. input(fieldname): returns value of single user-provided input field

3.4.1.3. only(fieldname, [array,of,field,names]): returns array of all user-input for the specified field names

3.4.1.4. except(fieldname, [array,of,field,names]): inverse of only

3.4.1.5. exist(fieldName): returns boolean whether fieldName exists in the input

3.4.1.6. has(fieldName): return boolean whether fieldName exists and has value

3.4.2. User and request state

3.4.2.1. method(): return method (GET, POST, PATCH, etc) used to access this data

3.4.2.2. path(): returns the path(without domain) to access this page

3.4.2.3. url(): return the URL(with domain) to access this page

3.4.2.4. is(): return boolean whether current path fuzzy-matches a provided string (eg: /a/b/c will be match by is('*b*)

3.4.2.5. ip(): return user's ip

3.4.2.6. header(): return array of headers, or passing header name as parameter

3.4.2.7. server(): return array of $_SERVER

3.4.2.8. secure(): return boolean whether this page is loaded with https

3.4.2.9. pjax(): returns a boolean whether this page is loaded with Pjax

3.4.2.10. wantsJson(): return boolean whether this request expects Json response (by Accept params in Header)

3.4.2.11. isJson(): return boolean whether this request content is Json (by checking request's header Content-Type

3.4.2.12. accepts(): return boolean whether this page request accepts a given content

3.4.3. Files

3.4.3.1. file(): return array of all uploaded files

3.4.3.2. hasFile(): returns a boolean whether a file was uploaded at the specified key

3.4.4. ParameterBag: associate arrays, get a particular item using $bag->get

3.5. Persistence

3.5.1. request can also interact with session

3.5.2. flash(): save request input to session

3.5.3. flush(): wipes all previously flash input

4. Response Object

4.1. extends Symfony/HttpFoundation/Response

4.2. Using and creating Response Objects in Controllers

4.2.1. Setting Headers

4.2.2. Adding cookies

4.3. Specialize Response Types

4.3.1. View Responses

4.3.2. Download Responses

4.3.3. File Responses

4.3.4. Json Responses

4.3.5. Redirect response

4.3.5.1. aren't commonly call on response() helper

4.3.5.2. redirect()->route: redirect to a route

4.3.5.3. redirect()->action(): redirect to an action

4.3.5.4. back()->withInput(): redirect back with Input

4.3.6. Custom Response Macros

4.3.6.1. register macro in AppServiceProvider

4.3.6.2. public function boot(){ Response::macro('myMacro', function($content){ return response(json_encode($content))->headers['Content-Type' => 'application/json']); }

5. Middleware

5.1. Definition: layer wrapping around applications

5.2. Middleware can inspect/decorate request.response

5.3. Sample code for decorating response

5.4. Binding Middleware

5.4.1. Global Middleware

5.4.1.1. binding in `app\Http\Kernel.php`

5.4.1.2. sample code:

5.4.2. Route Middleware

5.4.2.1. define routeMiddleware in `app\Http\Kernel`

5.4.2.2. sample code:

5.4.2.3. binding sample code:

5.4.3. Using Middleware groups

5.4.3.1. bundles of middleware that makes sense to be together in specific context

5.4.3.2. two default middleware group: web and api

5.4.3.3. the two middleware groups are mapped in routes/api.php file

5.5. Passing Parameter to Middleware

5.5.1. Parameter can be passing from routes to Middleware

5.5.2. split middleware name with parameters by `:`, and split between parameters by ','

5.5.3. sample code: