Coding a PHP 7 Framework #1 - Code Workshop

In this series of articles, we'll walk through the coding of a PHP micro-framework. It is assumed you are fairly familiar with PHP and coding / developing in general, however we'll try to explain each step so that a novice could follow.

If you need more space to see the code, click "Hide Sidebar >" above the Article Categories box to the right ->

Requirements

Before we start coding anything, a few things need to be stated and sorted out:

The framework we'll be creating will utilise features only found in PHP version 7.1 and above.

We'll be using a LAMP stack, but you can transpose / modify to suite your development environment (WAMP, nginx etc).

Linux (CentOS)

Apache 2+

Needs mod_rewrite and PDO extensions enabled.

MariaDB or MySQL

PHP (aforementioned 7.1)

And that's about it. If you're unsure, then you can opt to use hosting such as https://www.one.com and purchase a cheap ".website" domain name to follow along.

Editor

Notepad++, with the NppFTP plugin is recommended for following this tutorial.

Directory Tree

Go ahead and in your root directory add the following files and folders:

do/

engine/

app_config.php

config.php

init.php

library/

Lectric/

lecPDO.class.php

controlAction.class.php

controller.class.php

doAction.class.php

doResponse.class.php

view.class.php

view/

public/

render.php

css/

style.css

img/

js/

template/

common/

header.php

content.php

footer.php

views/

index.php

index.php

.htaccess

Make sure to set the appropriate permissions on the folders (I recommend 711 recursively, only on folders, 644 for php files).

File encoding

Ensure all your PHP files are encoded as UTF8. Most code editors will display this information. For example, in notepad++, the encoding is displayed in the status bar at the bottom right of the window, and you can set the encoding to UTF8 from the "Encoding" menu.

Database

Run the SQL statement below to create some example tables and insert some test data for use later in the development.

It's important that all our table encoding is UTF8_bin, which will make our lives easier later.

CARVin it up!

Let's look at the definition of a framework:

"A software framework provides a standard way to build and deploy applications."

In the context of web applications, the basic pricipal is that most web apps are tools for viewing something, performing an action or generating a none-view response. Exactly what a framework does, generate a view, response or run an action is determined by the url. Thus, the framework we'll create is tentaively called a C-ARV framework, standing for Controller -> Action / Response / View.

Unlike MVC or other design patterns, the framework we'll create will lean into being web-oriented (not to say we won't try to keep separation of concerns!) and use the action/response/view methodology to route logic via a simple controller whose job it is to parse a URL.

Bootstrap

Ok, with the prep out of the way, let's get creating our first few files. We'll implement a very simple bootstrap whose job it is to send all requests through one file, being /index.php, who will in turn start the application by inclusion of other files. This allows us to implement a rudimentary level of security within our directory structure.

.htaccess

The htaccess file only has a few instructions. Firstly, we check to see if a specific file is being requested (for example, an image we want to use on a webpage), if not then the request is forwarded to /index.php. This will only work if mod_rewrite is on and working.

init.php's job is to go and find the framework configuration file /engine/config.php and then instantiate the controller class. For now, don't worry about what the controller is / does, or what the argument passed to it $lecDBH is, we'll get to that in due course.

And that's the bootstrap done! Now we have the basic setup to start into the meat of the framework in the next article, filling the /engine/config.php file with various settings and logic to allow our framework to function correctly.