Welcome to PHP Frameworking tutorial, I assume you already have basic object-oriented programming knowledge to start with. Internet is full of awesome things, those things are often not so amazing to build or maintain. Yes, those things are web applications (web sites), most of us use them, but very few of us make a living creating them, and for those very few who spend hours programming every day (or want to), this tutorial exists, and will teach you how to simplify building/maintaining your websites. Enough chattering, let's get to the point!
What is a framework?
Framework can be a solid platform to build on, basically set of libraries (code written by someone else, not you.. or even you!) to help you rocket start, your programing proccesses. Building websites requires huge amount of the same things to be done all over again. Creating folder structure, connecting to database, controlling user actions or displaying output to users. There's a lot of different frameworks out there, we'll learn how to build our own, in order to understand how it all works together.
Folder structure
Each project needs a proper folder layout, in order to keep our projects easily maintainable, it's a good practice to stabilize their folder structure. Our framework will have the following structure:
- core/
- controllers/
- models/
- views/
- libs
- services/
- helpers/
- vendor
- public/
- assets/
- index.php
We're most probably going to alter it a bit, based on our needs during the tutorial. As you may
have noticed, we are going to apply MVC pattern (Model-View-Controller
) to our project, it'll be
covered by those folders: controllers, models, views.
Folder libs will contain other useful code,
separated into different categories, such as helpers
, which help us with presentation logic, or
services
which we can re-use across our application for different tasks. Folder vendor
will contain
any 3rd party libraries we will use, those will be installed using composer, which is a package manager,
I'll keep that for later. Folder public
will be root of your applications, containing our main file to run
(index.php
), keeping assets
folder accessible by browser. At last but not least, core
folder to keep our
framework files.
What is Model-View-Controller?
It's a common way to structurize software, a pattern, enabling developers to adapt easily when
switching between projects. There are several different patterns as well, but MVC
is most probably the
most common across today's web apps.
Let's code
To start let's create public/
folder, and public/index.php
file in it.
public/index.php:
<?php
echo "Hello from my site!";
We can start a local server from public/ if we are on linux/osx machine:
$ php -S localhost:8080
And we have a live site running on http://localhost:8080
. Easy right? Now let's get to the tricky part.
As mentioned before we'll stick with MVC
pattern to keep our project easy-to-maintain later on. In
order to meet that requirement, we should not call "echo" from index.php, but we should create a file
in views
folder to hold displayable data.
views/hello.php:
<p>Hello from my site!</p>
In order to display our view
, let's use the following code.
public/index.php
<?php
require('../views/hello.php');
That's it, we've separated display data to the place where it should be, now let's get rid of direct "require" call by creating a classes to handle displaying views.
core/view/viewLoader.php:
<?php
class ViewLoader{
public function __construct($path){
$this->path = $path;
}
public function load($viewName){
if( file_exists($this->path.$viewName) ){
return file_get_contents($this->path.$viewName);
}
throw new Exception("View does not exist: ".$this->path.$viewName);
}
}
core/view/view.php:
<?php
class View{
public function __construct($viewLoader){
$this->viewLoader = $viewLoader;
}
public function display($viewName){
echo $this->viewLoader->load($viewName);
}
}
Now we have two classes, ViewLoader to handle view file loading, and View class to be used publicly for view loading. Let's apply them:
public/index.php:
<?php
require('../core/view/viewLoader.php');
require('../core/view/view.php');
$viewPath = __DIR__.'/../views/';
$viewLoader = new ViewLoader($viewPath);
$view = new View($viewLoader);
$view->display('hello.php');
First we load in our core
libraries, ViewLoader
and View
classes. Then we declare path to views
folder. Next we create instance of ViewLoader, passing views
folder path as constructor parameter, then we pass ViewLoader's instance to View
constructor, so our View instance will know how to load view files properly. Then we can call $view->display(viewName);
in order to display our previously created hello.php view!
Thanks for reading
In next tutorial we'll continue building up our framework, stay tuned. You can find me on Facebook or Twitter. Don't be afraid to contact me if you have any comments, or questions!
Click to read Part 2 - Routing & Autoloading & Configuration
Last updated 2024-01-11 UTC