Write PHP Like a Pro: Build a PHP MVC Framework From Scratch
4.6 (1,788 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
6,295 students enrolled

Write PHP Like a Pro: Build a PHP MVC Framework From Scratch

Learn to write faster, easier to maintain, more secure PHP websites by using a model-view-controller framework
4.6 (1,787 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
6,294 students enrolled
Created by Dave Hollingworth
Last updated 3/2019
English, Indonesian [Auto], 3 more
  • Polish [Auto]
  • Romanian [Auto]
  • Thai [Auto]
Current price: $48.99 Original price: $69.99 Discount: 30% off
5 hours left at this price!
30-Day Money-Back Guarantee
This course includes
  • 3 hours on-demand video
  • 7 articles
  • 40 downloadable resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
Training 5 or more people?

Get your team access to 4,000+ top Udemy courses anytime, anywhere.

Try Udemy for Business
What you'll learn
  • Separate application code from presentation code
  • Organise your PHP code into models, views and controllers in an MVC framework
  • Use namespaces and an autoloader to load PHP classes automatically
  • Use the Composer tool to manage third-party package dependencies and autoloading
  • Handle errors and exceptions in PHP and display more or less error detail based on the environment
  • Understand how MVC frameworks work, making it easier to learn an existing framework like Laravel or CodeIgniter
Course content
Expand all 66 lectures 02:56:52
+ Introduction: MVC concepts and development environment setup
6 lectures 17:45

An introduction to the course, including:

  • A summary of what you're going to learn
  • How to get the most out of the course
  • How to download the code and other resources with each video
  • How to contact me for help with anything at all
Preview 03:16

Understand why mixing application code and presentation code in the same PHP script is a bad idea, the problems it causes, and their solution.

Preview 03:51

Learn what the Model-View-Controller (MVC) pattern is, what each part does, and the advantages of using it.

Preview 03:08

Install and configure everything you need on your computer so you can follow along with the course: a web server that supports PHP and a database server like MySQL.

If you do have any problems at this stage, please don't hesitate to ask for help. For example, having Skype installed or a recent install of WIndows 10 could require a slight change to the configuration to get it working as shown in the video.

Preview 03:20

Begin creating the framework by creating the folders necessary on the web server, and configuring it so the code will be more secure.

Preview 03:49

If you're using AMPPS on Windows, there's an additional change you need to make to the Apache configuration to change the root of the web server (not necessary on Linux as shown in the video).

Addendum: Additional configuration for AMPPS on Windows
MVC concepts
4 questions
+ Routing: how URLs are processed in an MVC framework
6 lectures 14:20

Understand what a front controller is: how every request goes though the same script file, and how we get the request URL from the query string.

Preview 02:59

Configure the web server to remove the query string question mark from the URL, giving us pretty or vanity URLs.

Configure the web server to have pretty URLs

If you're using the Apache web server (the one that comes with AMPPS and XAMPP for example), and you're having problems getting the pretty URLs working, then it's possible that you need an additional line of code in your .htaccess file. This lecture details the small change that you need to make to your code.

Addendum: Possible additional configuration required for the Apache web server

Learn what the router component of the framework does, create the class, and require it in the front controller. Also learn the difference between require and include in PHP.

Create and require (not include) the router class

Learn how the router matches the request URL to controllers and actions using routes in a routing table, add this functionality to the framework, and add some routes.

Create the routing table in the router, and add some routes

Match the route coming from the query string to the routes in the routing table, and obtain the parameters to create the controller and run the action later.

Match the requested route to the list of routes in the routing table

Test your knowledge of how the routing component in a framework works.

Routing and front controller concepts
4 questions
+ Advanced routing: add simpler but more powerful routes
9 lectures 32:35

Learn how we're going to make routes more flexible and powerful by adding patterns.

Preview 01:34

Learn how to go from simple to complex string comparisons using regular expressions, specifically simple character matching and metacharacters.

Preview 04:47

Use special characters in regular expressions, specifically:

  • Markers for the start and end of a string
  • Repetition
  • Wildcards
  • Escaping
  • The case sensitive modifier
Using special characters in regular expressions: advanced pattern matching

Learn to use character sets and ranges in regular expressions, including negated character sets and ranges.

Write even more powerful regular expressions: use character sets and ranges

Extract one or more parts of a string using both numbered and named capture groups in regular expressions.

Extract parts of strings using regular expression capture groups

Create a regular expression for a fixed URL structure, and match the incoming URLs to that expression to get the controller and action.

Get the controller and action from a URL with a fixed structure

Learn how to use regular expressions to replace all or part of a string, including replacing using backreferences to capture groups.

Replace parts of strings using regular expressions

Add functionality to create routes with variables for the controller and action that can be placed anywhere in the route. Routes will now be regular expressions instead of fixed strings.

Get the controller and action from a URL with a variable structure

Add functionality to create any variables (not just controller and action) with custom formats to routes.

Add custom variables of any format to the URL

Test your knowledge of the regular expression concepts you've learnt in this section.

Regular expressions
10 questions

Test your knowledge of the advanced routing functionality added in this section.

Advanced routing
3 questions
+ Controllers and actions
12 lectures 35:21

Learn what controllers and actions are and how they fit into the framework.

Preview 01:29

Learn how to create objects of a class and run methods of an object based on variables. Also how to check a class exists and a method is callable.

How to create objects and run methods dynamically

Add the dispatching step to the router to create the controller object and run the action method based on the parameters obtained from the route.

Dispatch the route: create the controller object and run the action method

Learn how to use class namespaces to organise classes, allowing two classes to have the same name if needed.

How to better organise your classes by using namespaces

Learn how PHP loads classes, and how defining classes in separate files can allow us to define an autoload function that means classes are loaded automatically instead of having to be explicitly required.

Class autoloading: load classes automatically without having to require them

Add namespaces to the existing classes in the framework and an autoload function so they're required automatically when needed.

Load classes automatically: add namespaces and an autoload function

We're using the query string for routing, so to be able to use the query string for variables, remove them before the route is matched to the routing table.

Remove query string variables from the URL before matching to a route

Make all custom URL variables from the route available in all controllers, and add a base controller class that all controllers will extend.

Pass route parameters from the route to all controllers

Learn how PHP calls methods on an object, method visibility and the __call "magic" method for calling non-public or non-existent methods on an object.

The __call magic method: how to call inaccessible methods in a class

Modify controllers and add the __call method to allow action filters - run code before or after every single action in a controller.

Action filters: call a method before and after every action in a controller

Adding the action filters to the framework has introduced a potential security hole with the router that could allow users to run action methods when they shouldn't be able to.

In this lecture I explain in detail what the problem is, and how to fix it.

Updated code with the fix applied is attached to this lecture as a resource.

Addendum: fix for a potential security bug introduced with the action filters

Add an optional namespace parameter to the routes in the routing table to allow controllers to be organised in namespaces and subdirectories.

Organise controllers in subdirectories: add a route namespace option

Test your knowledge of the controller functionality we've added to the framework in this section, including namespace and autoloading concepts.

Controllers and actions
7 questions
+ Views
9 lectures 18:39
Learn what views are and how they fit into the framework.
Preview 01:00

Create a view file with just presentation code, a core view class to render it and call this render method in a controller to display the view on screen.

Display a view: create a class to render views and use it in a controller

Learn how HTML works, why it's necessary to escape certain special characters, how to display those characters on screen using HTML entities, and how to avoid cross-site scripting (XSS) attacks.

Output escaping: what it is, why do it, and how and when to do it

Pass data to the view from the controller, converting an array of an unknown number of values into simple variables for the view.

Pass data from the controller to the view

Learn how using a template engine can improve your view files with simpler syntax, autoescaping of variables and template inheritance.

Templating engines: what they are and how they can improve your PHP code

Add the Twig template engine to the framework and render a Twig template to show the differences in syntax.

Make views easier to create and maintain: add a template engine

Twig 2.0 is now available. If you install or upgrade to that version, you need to remove the following line from the front controller, public/index.php:


This step is no longer necessary in Twig 2.0, and will cause an exception if you leave it in.

Addendum: Changes in Twig 2.0

A recent update to the Twig template library might cause an error when rendering a template.

You might see an error like this:

Fatal error
Uncaught exception: 'ErrorException'
Message: 'file_get_contents(App/Views/Home/index.html): failed to open stream: No such file or directory'

The problem occurs when creating the Twig environment and passing in the directory where the Twig templates are located.

This article tells you how to fix it.

Addendum: How to easily fix a potential exception when rendering a Twig template

Add a base view template and change the existing views so they inherit from it, removing repetition in the views.

Remove repetition in the view templates: add a base template to inherit from

Test your knowledge of the view functionality we added to the framework in this section.

4 questions
+ Manage code using Composer
7 lectures 17:09

An introduction to Composer: a tool to install and manage third-party code packages.

Preview 02:59

Learn how Composer is used to install third-party code packages.

Installing and using Composer

Install the Twig template engine using Composer.

Install the template engine library using Composer

Use the autoloader provided by Composer to automatically require all classes in third-party packages.

Include all package classes automatically using the Composer autoloader

Require the classes from the Twig template engine library using the Composer autoloader instead of calling the Twig one directly.

Use the Composer autoloader to load the template engine library

Use Composer to autoload load any class, not just ones from third-party packages.

Add your own classes to the Composer autoloader

Use Composer's autoloader to load all the classes in the framework, replacing the autoload function in the front controller.

Replace the autoload function with the Composer autoloader

Test your knowledge of managing third-party code packages and autoloading using the Composer tool.

4 questions
+ Models
6 lectures 13:42
Learn what models are and how they fit into the framework.
Preview 01:43

Create a sample database in MySQL, learn what data are needed to connect to a database from a script, and check the connection details are ok.

Create a database and check you can connect to it from PHP

Learn what PHP Data Objects (PDO) is and how it makes using databases in PHP much easier by seeing some examples.

An introduction to PDO: why it makes working with databases in PHP easier

Add a model class to the framework, connect to the database in it, retrieve some data and pass it to a view for display.

Add a model, get data from the database and display it in a view

Learn the best place to connect to the database so that the connection is reused and only used when needed.

Optimise the database connection: connect only on demand and reuse it

Oops! In the previous lecture, there's a slight error in the code shown in the video. Details of the error and the correction are detailed here.

Erratum: Correction to the Optimise the database connection code
Test your knowledge of the models functionality we added to the framework in this section.
4 questions
+ Configuration and error handling
8 lectures 20:44

Extract configurable settings (e.g. database connection details) out to a separate file, allowing configuration to be easily changed between environments (e.g. between development and production).

Put application configuration settings in a separate file

Learn what happens when something goes wrong in PHP code, the difference between errors and exceptions and how to write code that handles errors gracefully.

How PHP reports problems: errors, exceptions, and how to handle them

Add handlers to the framework to handle both errors and exceptions.

Handle errors: convert errors to exceptions and add an exception handler

Learn how to view current PHP configuration settings, and how to change them, either in php.ini or in code.

PHP configuration settings: where to find them and how to change them

Configure PHP to make sure all errors and exceptions are shown when they occur.

Configure PHP to display error messages

Add a configuration setting to display detailed error messages when developing, but a simple message in production whilst saving the error message to a logfile.

Show detailed error messages to developers, friendly error messages to users

Learn how HTTP uses status codes and how to classify errors in the framework by using them.

Categorise different types of error using HTTP status codes

Add view templates to display when errors occur in production, based on the error status code.

Add views to make error pages look nicer in production

Test your knowledge of how PHP handles errors and exceptions, and the functionality added to the framework in this section for just that.

Configuration and error handling
6 questions
+ Conclusion
3 lectures 06:37

Now that you know how a framework works, learning an existing one like Laravel will be easier. Discover some of the most popular frameworks and some of their advantages and disadvantages.

A brief introduction to some popular frameworks

A summary of everything you've achieved on the course, and where to go from here.

Bonus Lecture: Discounts on other courses
  • You should already be familiar with PHP and HTML.
  • You should be comfortable installing software on your computer - in the first section we'll be installing a web server.

Learn the basic concepts of using a model-view-controller framework that will make your PHP projects faster, easier to write and maintain, and more secure.

Learn to Structure your PHP Code Like a Professional by Building a PHP MVC Framework from Scratch.

  • Model-view-controller (MVC) pattern concepts
  • Build an MVC framework in PHP from scratch
  • Separate application code from presentation code
  • Use namespaces and an autoloader to load classes automatically

Take your PHP Projects to the Next Level

Learning how to use an MVC framework puts a very powerful tool at your fingertips. Most commercial websites and web applications written in PHP use some sort of framework, and the MVC pattern is the most popular type of framework in use.

The gap between knowing PHP and using a framework can be huge. This course bridges that gap. By writing your own framework from scratch, you'll gain an understanding of just how each component works. Frameworks like Laravel, Symfony and CodeIgniter all use the MVC pattern, so understanding how an MVC framework is put together will give you a strong background to more easily learn frameworks such as these.

Content and Overview

I designed this course to be easily understood by programmers who know PHP but don't know how to use a framework. Are you putting database code and HTML in the same PHP script? Want to know why this is a bad idea? Want to know how to do it better?

Starting with the basic concepts of MVC frameworks, this course will take you through all the steps needed to build a complete MVC framework, a piece at a time.

Beginning with a single PHP script, each lecture explains what you're going to add to the code and why, building up the framework step by step. At the end of this course, you'll have built a complete MVC framework in PHP, ready to use in your own projects.

Complete with working source code at every stage, you'll be able to work alongside the instructor and will receive a verifiable certificate of completion upon finishing the course.

Who this course is for:
  • This PHP MVC course is meant for those who already know PHP but want to know how they can improve their code by using a framework. This course is not for you if you don't already know PHP.
  • The gap between learning PHP and using a framework can be large - if you've just learnt PHP and you're wondering what the next step might be, this course is for you.