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 (124 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.
488 students enrolled
$30
Take This Course
  • Lectures 62
  • Contents Video: 3 hours
    Other: 1 min
  • Skill Level All Levels
  • Languages English, captions
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 2/2016 English Closed captions available

Course 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.

What are the 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.

What am I going to get from this course?

  • 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

What 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.

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Introduction: MVC concepts and development environment setup
03:16

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

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

03:08

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

03:20

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.

03:49

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

Article

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).

MVC concepts
4 questions
Section 2: Routing: how URLs are processed in an MVC framework
02:59

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.

02:46

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

02:36

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.

02:12

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.

03:05

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.

4 questions

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

Section 3: Advanced routing: add simpler but more powerful routes
01:34

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

04:47

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

03:57

Use special characters in regular expressions, specifically:

  • Markers for the start and end of a string
  • Repetition
  • Wildcards
  • Escaping
  • The case sensitive modifier
02:33

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

02:58

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

05:36

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

02:49

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

05:30

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.

02:51

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

10 questions

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

3 questions

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

Section 4: Controllers and actions
01:29

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

01:48

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.

04:34

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.

03:47

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

03:11

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.

02:54

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

02:25

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.

02:32

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

02:24

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.

04:48

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

02:27

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

7 questions

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

Section 5: Views
01:00
Learn what views are and how they fit into the framework.
02:15

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.

04:30

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.

01:55

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

03:27

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

02:22

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

02:20

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

4 questions

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

Section 6: Manage code using Composer
02:59

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

03:34

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

01:59

Install the Twig template engine using Composer.

01:46

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

01:03

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

02:51

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

02:57

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

4 questions

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

Section 7: Models
01:43
Learn what models are and how they fit into the framework.
02:58

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.

03:27

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

02:16

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

02:51

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

Article

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.

4 questions
Test your knowledge of the models functionality we added to the framework in this section.
Section 8: Configuration and error handling
02:15

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).

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

02:53

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

02:02

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

01:57

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

03:12

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.

03:14

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

01:56

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

6 questions

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

Section 9: Conclusion
03:13

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.

02:54

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

Bonus Lecture: Get discounts on my other Udemy courses
Article

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

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!

Ready to start learning?
Take This Course