Write PHP Like a Pro: Build a PHP MVC Framework From Scratch
4.7 (339 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
1,245 students enrolled
Wishlisted Wishlist

Please confirm that you want to add Write PHP Like a Pro: Build a PHP MVC Framework From Scratch to your Wishlist.

Add to Wishlist

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.7 (339 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
1,245 students enrolled
Created by Dave Hollingworth
Last updated 2/2017
English
English
Price: $70
30-Day Money-Back Guarantee
Includes:
  • 3 hours on-demand video
  • 6 Articles
  • 39 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
What Will I 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
View Curriculum
Requirements
  • 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.
Description

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 is the target audience?
  • 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.
Students Who Viewed This Course Also Viewed
Curriculum For This Course
Expand All 65 Lectures Collapse All 65 Lectures 02:53:38
+
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
00:21

MVC concepts
4 questions
+
Routing: how URLs are processed in an MVC framework
6 Lectures 14:23

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
02:46

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
00:45

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
02:36

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
02:12

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
03:05

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
03:57

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
02:33

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
02:58

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
05:36

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
02:49

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
05:30

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
02:51

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
11 Lectures 32:19

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
01:48

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
04:34

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
03:47

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
03:11

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
02:54

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
02:25

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
02:32

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
02:24

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
04:48

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
02:27

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:42
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
02:15

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
04:30

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
01:55

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
03:27

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
02:22

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:

Twig_Autoloader::register(); 

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
00:11

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
00:42

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
02:20

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

Views
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
03:34

Install the Twig template engine using Composer.

Install the template engine library using Composer
01:59

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

Include all package classes automatically using the Composer autoloader
01:46

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
01:03

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

Add your own classes to the Composer autoloader
02:51

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
02:57

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

Composer
4 questions
+
Models
6 Lectures 13:48
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
02:58

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
03:27

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
02:16

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
02:51

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
00:33

Test your knowledge of the models functionality we added to the framework in this section.
Models
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
02:15

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
03:15

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

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

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
02:02

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

Configure PHP to display error messages
01:57

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
03:12

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
03:14

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
01:56

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:29

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
03:13

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

Conclusion
02:54

Bonus Lecture: Get discounts on my other Udemy courses
00:22
About the Instructor
Dave Hollingworth
4.7 Average rating
514 Reviews
2,827 Students
6 Courses

Hi, I'm Dave Hollingworth. I'm an IT trainer and web application developer. I've been programming for over twenty-five years, and teaching IT since 1999.

I've developed enterprise-level applications on platforms ranging from mainframes to mobiles: from my first web application, a quotes system created for a major insurance company back in 1999 - the first of its type in the sector - to interactive learning management systems for online language learning.

I've taught courses ranging from basic use of email to advanced web application development, both online and in-person. I've been teaching on Udemy since 2012.

I'm passionate about technology, and love teaching it! I believe it's possible to explain even the most complex subjects in a simple, straightforward way that doesn't confuse the student.

I look forward to welcoming you onto one of my courses!