Unrivaled Creations Stories

Internet Website Design and Bespoke Software Application Development with PHP and the Laravel Stack.

Understanding Laravel Architecture Concepts

1 year ago · 4 MIN READ
#development  #laravel 

When first installed, the Laravel framework file hierarchy and execution order can leave newcomers confused and lost. Let's take a journey into its architectural concepts for beginners.

Learn about Laravel’s architecture.

Let me begin by saying I am a beginner with Laravel; just not with web application development (specifically with PHP). When I refer to newcomers and beginners, I speak to that context: Newcomers and beginners to Laravel, but not to programming with the PHP language or to web development in general. So if you're an experienced programmer looking into Laravel or a Laravel-derived app, like Canvas or Coaster CMS, this article's a good place to start.

First Impressions

As I delved into the world of Laravel for app development, I was struck by a couple of memorable first impressions: (1) The framework is really rather complete and highly optimized right out of the box; and (2) The directory structure seemed misaligned with respect to both the organization of it’s files by purpose, order of execution and by authorship (e.g., with most frameworks I've made myself or worked with in the past, framework files go “here” and my files go “there”). Even still, as with "everything Laravel," it all makes sense in the end; but it was a little disconcerting for me at first.

Laravel Architecture in a Nutshell

A Laravel application’s architecture can be summarized as follows:

  1. The “entry point” (/public/index.php)
  2. The “service container” (an instance of which is created in the bootstrapping process and is perhaps better called the “Laravel Application”)
  3. The "kernel" (an instance of which is created by the “service container”)

The "entry point" is super-easy to understand, because it's just one file with only a few odd lines of code. It gives you one nice, easy file to point your web server to.

So what (and where) is the "service container?" What (and where) is the "kernel?" Let’s take a look at the bootstrap process and the directory structure to find out.

The Bootstrap Process

"Bootstrapping" in this context means "to get yourself up and going." For newcomers to the framework, Laravel bootstrapping is difficult to understand at first; and when you're going to write an app for a customer, it's a little scary handing over deliverables when you don't know what's in the secret sauce. Rest assured, there is no ghost in the machine. Laravel can do a lot of stuff, but it does nothing you don't tell it to do. It's just a framework and it can be trusted. Hopefully, you'll feel better about that after a short study about what's going on “under the hood.”

The heart of the Laravel framework's optimizations lay with the cleverness of the bootstrapping process. Framework services are only loaded when the services they provide are required and it all happens automatically. As a developer, you don't have to lift a finger to effect this optimization. It just happens. That's cool. And it makes your app light weight (in terms of memory consumption) and lightening fast. In brief, the bootstrap process “registers” a catalog of framework components but doesn't actually load them unless you decide to use one. (more advanced developers might recognize the insertion of a declarative programming paradigm here, but that's a topic for a future story.)

The Relevant Directory Structure

At first, the directory structure didn't make sense to me. In some ways, it still doesn't; but I've learned to make friends with it and understand why it's set up the way it is.

Here's a summary of the most important files and directories and what they do:

  1. /public/index.php: This is the "entry point" that gives the web server a target to receive and process HTTP requests and send HTTP responses.
  2. The /bootstrap directory: The /public/index.php entry point calls the scripts in the bootstrap directory to prepare your application for use. /bootstrap/autoload.php loads the Composer dependency files (defined by composer.json) and /bootstrap/app.php creates an instance of the Laravel “service container.” The “service container” is more or less a fancy name for the "Laravel application" and is the second critical component in Laravel's architecture. The Laravel “service container” resides in the /vendor/laravel/framework/src/Illuminate/Foundation directory (Application.php to be exact).
  3. The /app/Http/Kernel.php file: The Laravel service container, once created, passes control of the HTTP request to the HTTP “kernel,” which returns a response.

The Kernel

The "kernel" receives a request and returns a response.

That seems an understatement and certainly is an impressive generalization. The kernel configures error-handling, configures logging, loads middleware, loads the service providers, bootstraps all framework components, hands off the request to the router, and returns the HTTP response.

That kernel does a whole heck of a lot, but the two most important functions of the kernel are the service providers and the router. Your code will be handled by the kernel as well, so the kernel is therefore the heart of your application.

Service Providers

According to Laravel's documentation, bootstrapping the service providers is the most important function performed by the kernel since this bootstraps and configures every feature offered by the framework. The service providers to be bootstrapped are listed in the /config/app.php file.

The Router

In Laravel, the router pairs a request with a response. After the service providers have been bootstrapped and configured by the kernel, the application’s request is handed off to the router for dispatching. We are finally reaching a point where your code and your application or website comes into play!

The router pulls its routes from the configuration files in the /routes directory. /routes/web.php, for example, defines the routes for HTTP requests. If you’ve managed to build a simple Laravel application, you’ve probably already worked with the routes. Routing a request is one of the last things the kernel does before it sends a response (e.g., a website) back to the requester (e.g., the browser). What it does in between, is run your code.

Where Do You Fit In?

Your code will be handled by the kernel, once the router satisfies any middleware requirements (like user name and password authorization) and decides which route or controller code needs to run. Your application code, therefore, runs from the kernel in terms of Laravel's architecture.

Concluding Remarks

Hopefully, this article will help you understand some of Laravel’s unique terminology and know where those important files are located. Writing this story helped me more fully understand how a Laravel application is constructed and how its complex pieces fit together. I hope it will help developers learning Laravel strengthen their knowledge of this incredibly powerful web application framework.


Michael Hall

Hi! I am a digital product designer and website/web application developer always seeking a better version of myself. Follow my journey as I share my story (and expertise) through the mutable, ever-changing, ever-growing world of design, web development and technology.
comments powered by Disqus

Copyright © 2017 by Michael Hall. All rights reserved. · Sign In