TABLE OF CONTENTS
1. Introduction2. Routing basics3. Middleware4. CSRF Protection5. Controllers6. HTTP Requests7. HTTP Responses8. Views9. Blade Templates10. Conclusion 11. CloudThat 12. FAQs1. Introduction
We covered the key features of Amazon Laravel in the previous part of Web Application Development Made Simple and Productive with Laravel Framework on AWS Part 1. This second part will explain the basic components of AWS Laravel.
2. Routing basics
Laravel routing is a simple and expressive way to define routes and behavior. It does not require complex routing configuration files. Laravel routes can accept a URI as well as a closure.
use Illuminate\Support\Facades\Route; Route::get(‘/greeting’, function () return ‘Hello World’;);12345use Illuminate\Support\Facades\Route; Route::get(‘/greeting’, function () return ‘Hello World’;);When multiple routes needed to be shared with the same URI, routes use the get, post, put, patch, delete, and options methods. It should be defined before routes using any, match, or redirect methods to ensure that the incoming request matches the correct route.
Middleware uses a mechanism to inspect and filter HTTP requests that are sent to it. Middleware can be thought of as a series “layers” of HTTP requests that must pass before they reach your application. Each layer can inspect the request and reject it completely.
Define Middleware – The following command can be used to create new middleware:
php artisan make:middleware EnsureToken1php artisan make:middleware EnsureToken
4. CSRF Protection
Cross-Site Request Forgery is a type attack where attackers send requests to a system with an authorized user pretending to be trusted by it.
Laravel offers protection against CSRF attacks through the generation of a CSRF token. This CSRF token for each user is generated automatically. This token is nothing more than a random string that the Laravel application manages to verify user requests. This can be done in three ways.
1. @csrf() .2. csrf_field() . 3. csrf_token() .1231. @csrf() .2. csrf_field() . 3. csrf_token() .
In addition to checking for the CSRF token as a POST parameter, the App\Http\Middleware\VerifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header. For example, you could store the token in an HTML tag.
Instead of defining all the request handling logic in closures in route files, it’s better to organize this using “controller” classes. Controllers are used to group related request handling logic within one class. They are stored in the app/Http/Controllers directory of the Laravel project.
A base class is not mandatory for controllers. However, without it, we won’t have access to useful features like the middleware, authorize methods, and so on.
6. HTTP Requests
Laravel’s ‘Illuminate\Http\Request’ class provides an object-oriented method to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request. There are many types of requests.
Requests for PSR-7
Request an IP Address
Request Path & Method
7. HTTP Responses
Laravel offers different ways to send back responses. It can be sent from a controller or a route. Below is the basic response. It returns a string from either a controller or route. This string will be converted automatically by the Laravel framework into an HTTP response.
Strings returning from controllers and routes can also be arrays. The framework will convert the array into