How does Laravel work? A crystal clear explanation.
Introduction to how Laravel works
Laravel is a framework based on PHP, which enables developers to build web applications faster. It provides us with tons of pre-written PHP code that lets us focus on our goals instead of reinventing the wheel.
But do you know exactly how it works?
From the moment a user clicks a link to your site, to when the data pops up on their screen, let me give you a tour on how Laravel orchestrates this web symphony.
How Laravel works in tandem with PHP and the web server
Step 1: A user makes a request
Imagine someone clicks a link to a page on your website. That’s a request, and it’s the starting point of our journey.
Step 2: The web server takes over
The request first arrives at the web server, like Nginx or Apache. This is basically the doorman of your website, deciding where each request should go.
Step 3: Passing the baton to PHP
If the web server sees that this request needs some dynamic action (like fetching blog posts from a database and displaying them), it hands the request to PHP. PHP is the scripting language that’s going to execute server-side logic.
Step 4: Laravel enters the scene
Since we need PHP to fulfil this request, we also need Laravel. As an user of this framework, your code takes advantage of it. Therefore, Laravel picks up the request and uses its “routes” to determine what code should run. For instance, if the /blog path is requested, it would be a shame to run the code for the forum or whatever, right? 😅
Step 5: Business logic & data manipulation
Your Laravel application will then do whatever you instructed it to do—fetch data, perform calculations, you name it. This is the “business logic” part, and it’s often where your PHP coding skills come into play.
Step 6: Crafting a response
After running the necessary code and getting the required data, Laravel creates a response. This can be a web page, some JSON data, or anything else.
Step 7: PHP says goodbye
PHP wrap up this response and gives it back to the web server.
Step 8: Back to the user
Nginx receives the prepared response from PHP and forwards it to the user’s browser. Voilà! The page loads, and the user sees the content.
Now, you saw when and what role Laravel plays in this process!
What problems does Laravel solve?
Imagine you’re building a house. You could create every single element like nails, screws, and wooden planks—from scratch, but that would be incredibly time-consuming. Instead, you’d go to a hardware store and buy these items or it’d take forever to complete your project, right?
Laravel is like that hardware store but for web developers. And even better: it’s free! Here are some of the components it provides:
- Routing, which is the system that redirects the user to the relevant code. If a user goes to https://example.com/contact, we don’t want to run the code for the forum. 😅
- Authentication, offering you secure user-tied features.
- Eloquent, a database interactions layer, making it easier to do any operation on your databases by writing PHP code instead of SQL.
- Blade, a template engine allowing you to easily separate your HTML markup from your PHP code.
- Testing helpers, that enable developers to write tests so much more easily than with any other PHP framework.
- And much more like caching, file storage, emails, notifications, task scheduling, etc.!
Summing up how Laravel works
So, in a nutshell, Laravel is a feature-packed PHP framework that makes web development faster, easier, and more fun. Whether you’re a newbie just starting out or an experienced developer looking for something robust, Laravel probably is the answer.
I hope you will create something amazing!
I've been working with Laravel for about 6 years now, but never read the framework code. Been doing that for a few days now and it is both a fun and educational process (although not very efficient if you just want to learn how to use it, and not how it was built and how it works). It also requires some basic knowledge, but it feels really good to understand exactly how things work. I'm documenting that on youtube here, would love to share ideas with others that might find this interesting.