If you've not used a PHP framework before, then you'll likely be used to having a bunch of different PHP files in your web directory. The person using your web application will request each script individually.
Laravel uses a front-controller and router combination. What this means is that there's only a single PHP file in your web root directory, and all requests to your application will be made through this script. This file is called index.php
and you'll find it in the public
directory, which is the only directory that should be accessible on the web.
I can't make a web application with one page!
Don't worry. We've got a solution to that. You see, Laravel uses some different techniques to serve different content based on the state of the web-browser request. In fact, here's a diagram to display the lifecycle of a single request to a Laravel framework application.
Request > Services > Routing > Logic > Response
In a way, a request to a webserver is an input/output loop. In honesty, there are a few more steps involved, but this isn't the place to discuss them! Let's step through each section of the process, shall we?
Request
Every request made by a web browser to your application has a broad range of information attached to it. For example, the URL, the HTTP method, request data, request headers, and information about the source of the request.
It's up to Laravel, and your application to interrogate the information within the request, to decide which action to perform. Using Laravel, the information for the current request is stored in an instance of the class Illuminate\Http\Request
, which extends from the Symfony Framework Symfony\Component\HttpFoundation\Request
class.
You see, the Symfony Framework has a fantastic implementation of the HTTP protocol in its ‘HTTP Foundation’ package. Laravel makes use of this package to avoid re-inventing a wheel.
We now have a request, and so we have all the information we need for our application to decide on an appropriate action. So, what's next? Let's take a look.
Services
The next step in the process is the bootstrapping of the framework. Laravel ships with a bunch of services and features that make our lives as web developers considerably easier! Before we can make use of these services, they need to bootstrapped.
The framework will load all defined services and configuration, and ensure that it has everything it needs to support our code. We'll take a closer look at how services are loaded within the 'Service Providers' chapter.
The framework is now ready for our code, but which piece of code should it run? Let's find out!
Routing
As we have discovered previously, there's only a single script that's accessible when using Laravel. How do we show different pages, or make different actions? That's where the router makes an appearance!
The router's sole purpose is to match up a request to the appropriate piece of code to execute. For example, the router will know that it should run the code to display a users profile page when the request includes a HTTP verb of ‘GET’ and a URI of ‘/user/profile’.
Laravel has a nice way of defining these routes, and we'll be looking at that very soon. For now, let's take a look at what happens next.
Logic
Next, we have our logic segment. This section can best be described as your code. It's where you'll be talking to a database, validating forms, or showing pages.
In Laravel, we've got some different ways of defining your logic, but we'll be looking at this in a later chapter. For now, let's take a look at the final section, shall we? I know our application users will be eager for this one!
Response
The response is created at the end of your logic. It might be a HTML template. It might be some JSON data; it might just be a string. Hey, it might be nothing at all. In some sad circumstances, it might be an error screen or a 404 page! It's good to have options, isn't it?
The response is what your application users will see. It's the part that they are most excited about!
Let's summarize, shall we?
The web browser sends a request. Laravel bootstraps its services, interrogates the request, and performs routing operations. Our code is executed and delivers a response to the user. It's a wonderful smooth operation, isn't it?
It sounds like a very simple process, but I promise that keeping these “flows” in your mind, will make you a better web developer.
Let's turn up the music, and keep this party going in the next chapter! We're going to be looking at the MVC (Model View Controller) software architecture pattern. It's used by plenty of Laravel apps, so I think it's worth reading!
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new and exciting books!