Understanding The Basic Components Of Laravel Framework – Part 2

April 22, 2022 | Comments(0) |

TABLE OF CONTENT

1. Introduction
2. Routing Basics
3. Middleware
4. CSRF Protection
5. Controllers
6. HTTP Requests
7. HTTP Responses
8. Views
9. Blade Templates
10. Conclusion
11. About CloudThat
12. FAQs

1. Introduction

In the previous part of Web Application Development Made Easy And Productive With Laravel Framework on AWS – Part 1, we went through the key features of Amazon Laravel. In this second part, we will understand the Basic components of AWS Laravel in brief.

2. Routing Basics

Laravel routing has a straightforward and expressive method of defining routes and behavior without complicated routing configuration files. Laravel routes accept a URI and a closure:

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 the any, match, and redirect methods which ensure the incoming request is matched with the correct route.

3. Middleware

Middleware has a mechanism that filters and inspects HTTP requests to enter into the application. It’s best to envision middleware as a series of “layers” HTTP requests that must pass through before they hit your application. Each layer can examine the request and even reject it entirely.

Define Middleware –
New middleware can be made using the below command:

4. CSRF Protection

Cross-Site Request Forgery (CSRF) is a type of attack in which attackers send requests to the system with an authorized user who supposes to be trusted by the system.

Laravel provides protection against CSRF attacks by generating a CSRF token. This CSRF token is generated automatically for each user. This token is nothing but a random string that is managed by the Laravel application to verify the user requests. There are three different ways in which you can do this.

  • X-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. You could, for example, store the token in an HTML meta tag:

5. Controllers

Rather than defining all of the request handling logic as closures in the route files, there’s a better option to organize this using “controller” classes. Controllers are meant to group associated request handling logic within a single class. In the Laravel project, they are stored in the app/Http/Controllers directory.

AWS Laravel

Controllers are not required to extend a base class. But, without it, we won’t have access to convenient features such as the middleware and authorize methods, etc.

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 different types of requests –

PSR-7 Requests

AWS Laravel

Request IP Address

AWS Laravel

Request Path & Method

AWS Laravel

Request Headers

AWS Laravel

7. HTTP Responses

Laravel provides different ways to return responses. It can be sent either from a route or from the controller. The basic response is given below which returns a string from a route or controller. The Laravel framework will convert this string automatically into a full HTTP response.

AWS Laravel

Additionally, strings returning from routes and controllers can also be arrays, and the framework will convert the array into a JSON response.

AWS Laravel

We may also return Eloquent ORM models and collections directly from the routes and controllers. In this case, also Laravel will automatically convert the models and collections to JSON responses while respecting the model’s hidden attributes.

AWS Laravel

8. Views

Generally, the view contains the HTML but Laravel doesn’t return the entire HTML documents strings directly from the routes and controllers. It provides a convenient way to place all of the HTML in separate files. A simple greeting view template would look like this-

AWS Laravel
Views are stored at ‘resources/views/greeting.blade.php’, we may return it using the global view helper like so-

AWS Laravel

Creating a view is very simple it can be done by placing a file with the .blade.php extension in the application’s resources/views directory. The .blade.php extension informs the framework that the file contains a Blade template. Blade templates contain HTML as well as Blade directives that allow us to easily echo values, create “if” statements, iterate over data, loops, and more.

9. Blade Templates

The blade is a templating engine which simple, yet powerful and it’s included with Laravel. Unlike some other PHP templating engines, Blade doesn’t restrict us from using core/plain PHP code in the blade template. Even if all blade templates are compiled as core/plain PHP code, they are cached also until modified. By all means, the blade adds essentially zero overhead to the application.

AWS Laravel

  • Blade & JavaScript Frameworks-
    As many JavaScript frameworks also use “curly” braces to indicate a given expression so in order to encounter that we can use @ symbol to inform the Blade rendering engine which expression should remain untouched.
    AWS Laravel

In the above example, the @ symbol will be removed by the Blade, however, {{ name }} expression will remain untouched by the Blade engine, allowing it to be rendered by the JavaScript framework.

10. Conclusion:

Laravel is a fantastic framework and no wonder that it has become the go-to application framework for PHP. I hope that you have a solid foundation in the fundamentals of building Laravel applications on AWS by now. But of course, we’ve just scratched the surface—now that you have gotten started with Laravel, you can dive deeper into the framework. You’ll learn something new with every project!

Stay tuned for a follow-up blog on “Laravel Basics.” We will be covering URL Generation, Session, Validation, Error Handling, Authentication & Authorization.

11. About CloudThat

CloudThat is the official AWS Advanced Consulting Partner, Microsoft Gold Partner, and Training partner helping people develop knowledge on the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

If you have any queries about AWS Laravel components, Laravel framework development, or any other AWS services, feel free to drop in a comment and we will get back to you quickly.

12. FAQs

Q1: Can I learn Laravel without knowledge of PHP?

A: Laravel is PHP’s framework. It will be really difficult to understand the functions and building blocks of Laravel without having prior knowledge of PHP.

Q2: Is it worth learning AWS Laravel in 2022?

A: Laravel is still going strong and has a very active community of users. The market share will only grow in 2022 as more and more developers opt for Laravel as the framework of choice for their projects.

Q3: How soon can I learn Laravel?

A: It depends entirely on your learning schedule and willingness to learn. If you have knowledge of OOP and prior programming experience, it won’t take a lot of time to get used to Laravel for application development.


Leave a Reply