To bootstrap a PHP Web App Is Easier than You Think

Simone Lippolis
Simone Lippolis
Published in
3 min readJun 12, 2018
Source: Luca Bravo, Unsplash.com

I wrote a lot of PHP code for my personal projects. PHP is a solid language, and the current version support for Object-Oriented programming is full and easy to learn. The bulk of built-in functions covers almost every programming need, and the ability to easily access linux C libraries extends its power. Since Composer became a reality, finding and adding 3rd party packages to my projects became even easier.

What I felt I was missing, was a solid framework that allowed me to quickly start new applications up, a framework that forced a convention in writing code and that was able to manage some of the most boring tasks I always needed: managing routing, connecting to a DataBase, including and instantiating classes. In the past I worked with Phalcon and Cake, and while I understand their value for big corporate projects, I don’t think they’re agile enough for small projects. The coding standards that they enforce, also, are something that I don’t really like.

The framework I built is based on few assumptions:

  1. Routing is important and essential, not only for SEO-aware projects. A human-readable URL is useful for everyone
  2. The old-school php idea “one-URL-one-page” isn’t wrong, and helps write cleaner code by reducing the size of each file
  3. OOP is powerful and must be, if not enforced, recommended
  4. Composer must be enabled by default

Starting from these four points, I built a boilerplate that is based on two core files: settings.php contains all the required settings, including site title, DB connection info, routing info, etc.; index.php is the real core of the application. An apache VirtualHost rewrite rule (or, alternately, a .htaccess file) redirects all the requests to the `index.php` which:

  1. Checks the integrity of all required file
  2. Imports Classes from Composer
  3. Imports custom-defined Classes
  4. Checks the requested URL against the set of defined routes (routes can be defined as regular expressions, so they are extremely flexible)
  5. Includes the correct source file

The included file will then have access to all the Classes available and to all the system variables.

The latest version available has also support for CLI, this means that includes and initialises all the needed classes and libraries even if run via command-line. This feature is particularly important for me, because it allows me to run scrapers from command line while being able to access my entire collection of classes and settings variables without the need to perform ad-hoc includes or initializations.

The skeleton of my PHP Boilerplate is available on Github. On my personal projects I use an enhanced version of it, that uses an helper class to automatically instantiate Twig or Smarty if they are included via Composer.

I also created a separate repository for helper classes I usually include in my projects. They feature few static methods useful for very specific tasks; using them helps me maintain my code clean, compact, consistent, and readable.

My name is Simone Lippolis, after spending almost ten years as Design Technologist at frog, I am now with Cisco, as a Data Visualization Expert. This article is part of my online portfolio that you can access at: simonelippolis.com.

--

--

Simone Lippolis
Simone Lippolis

Data Visualization Practicioner in Milan, with a passion for connected objects and photography.