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` register: bind classes and aliases to container 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 all(): return array of all user-inputted input(fieldname): returns value of single user-provided input field only(fieldname, [array,of,field,names]): returns array of all user-input for the specified field names except(fieldname, [array,of,field,names]): inverse of only exist(fieldName): returns boolean whether fieldName exists in the input has(fieldName): return boolean whether fieldName exists and has value

3.4.2. User and request state method(): return method (GET, POST, PATCH, etc) used to access this data path(): returns the path(without domain) to access this page url(): return the URL(with domain) to access this page is(): return boolean whether current path fuzzy-matches a provided string (eg: /a/b/c will be match by is('*b*) ip(): return user's ip header(): return array of headers, or passing header name as parameter server(): return array of $_SERVER secure(): return boolean whether this page is loaded with https pjax(): returns a boolean whether this page is loaded with Pjax wantsJson(): return boolean whether this request expects Json response (by Accept params in Header) isJson(): return boolean whether this request content is Json (by checking request's header Content-Type accepts(): return boolean whether this page request accepts a given content

3.4.3. Files file(): return array of all uploaded files 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 aren't commonly call on response() helper redirect()->route: redirect to a route redirect()->action(): redirect to an action back()->withInput(): redirect back with Input

4.3.6. Custom Response Macros register macro in AppServiceProvider 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 binding in `app\Http\Kernel.php` sample code:

5.4.2. Route Middleware define routeMiddleware in `app\Http\Kernel` sample code: binding sample code:

5.4.3. Using Middleware groups bundles of middleware that makes sense to be together in specific context two default middleware group: web and api 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: