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

1. Request Object

1.1. extension from Symfony's HttpFoundation\Request

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

1.3. Getting Laravel Request

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

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

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

1.4. Get Basic Information about a Request

1.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

1.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

1.4.3. Files file(): return array of all uploaded files hasFile(): returns a boolean whether a file was uploaded at the specified key

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

1.5. Persistence

1.5.1. request can also interact with session

1.5.2. flash(): save request input to session

1.5.3. flush(): wipes all previously flash input

2. Response Object

2.1. extends Symfony/HttpFoundation/Response

2.2. Using and creating Response Objects in Controllers

2.2.1. Setting Headers

2.2.2. Adding cookies

2.3. Specialize Response Types

2.3.1. View Responses

2.3.2. Download Responses

2.3.3. File Responses

2.3.4. Json Responses

2.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

2.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']); }

3. Middleware

3.1. Definition: layer wrapping around applications

3.2. Middleware can inspect/decorate request.response

3.3. Sample code for decorating response

3.4. Binding Middleware

3.4.1. Global Middleware binding in `app\Http\Kernel.php` sample code:

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

3.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

3.5. Passing Parameter to Middleware

3.5.1. Parameter can be passing from routes to Middleware

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

3.5.3. sample code:

4. Laravel's request Lifecycle

4.1. Request coming to app via Middleware

4.2. Middleware decorate the request

4.3. Application resolve the request and makes response

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

4.5. User receive response

5. Bootstrapping the Application

5.1. The flow of bootstrapping as above

5.2. kernel is core router of every Laravel Application

5.2.1. Web kernel

5.2.2. Console kernel (for console commands)

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

5.3. Service Provider

5.3.1. class that encapsulates logic that application need to run

5.3.2. ServiceProvider have two important methods: `boot` and `register` register: bind classes and aliases to container boot: binding event listener, defining routes

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