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.
Structure your PHP Code Like a Professional by Building a PHP MVC
Framework from Scratch.
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.
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
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.
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.
An introduction to the course, including:
Understand why mixing application code and presentation code in the same PHP script is a bad idea, the problems it causes, and their solution.
Learn what the Model-View-Controller (MVC) pattern is, what each part does, and the advantages of using it.
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.
Begin creating the framework by creating the folders necessary on the web server, and configuring it so the code will be more secure.
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).
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.
Configure the web server to remove the query string question mark from the URL, giving us pretty or vanity URLs.
If you're using the Apache web server (the one that comes with AMPPS andfor 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.
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.
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.
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.
Test your knowledge of how the routing component in a framework works.
Learn how we're going to make routes more flexible and powerful by adding patterns.
Learn how to go from simple to complex string comparisons using regular expressions, specifically simple character matching and metacharacters.
Use special characters in regular expressions, specifically:
Learn to use character sets and ranges in regular expressions, including negated character sets and ranges.
Extract one or more parts of a string using both numbered and named capture groups in regular expressions.
Create a regular expression for a fixed URL structure, and match the incoming URLs to that expression to get the controller and action.
Learn how to use regular expressions to replace all or part of a string, including replacing using backreferences to capture groups.
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.
Add functionality to create any variables (not just controller and action) with custom formats to routes.
Test your knowledge of the regular expression concepts you've learnt in this section.
Test your knowledge of the advanced routing functionality added in this section.
Learn what controllers and actions are and how they fit into the framework.
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.
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.
Learn how to use class namespaces to organise classes, allowing two classes to have the same name if needed.
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.
Add namespaces to the existing classes in the framework and an autoload function so they're required automatically when needed.
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.
Make all custom URL variables from the route available in all controllers, and add a base controller class that all controllers will extend.
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.
Modify controllers and add the __call method to allow action filters - run code before or after every single action in a controller.
Add an optional namespace parameter to the routes in the routing table to allow controllers to be organised in namespaces and subdirectories.
Test your knowledge of the controller functionality we've added to the framework in this section, including namespace and autoloading concepts.
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.
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.
Pass data to the view from the controller, converting an array of an unknown number of values into simple variables for the view.
Learn how using a template engine can improve your view files with simpler syntax, autoescaping of variables and template inheritance.
Add the Twig template engine to the framework and render a Twig template to show the differences in syntax.
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.
A recent update to the Twig template library might cause an error when rendering a template.
You might see an error like this:
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.
Add a base view template and change the existing views so they inherit from it, removing repetition in the views.
Test your knowledge of the view functionality we added to the framework in this section.
An introduction to Composer: a tool to install and manage third-party code packages.
Learn how Composer is used to install third-party code packages.
Install the Twig template engine using Composer.
Use the autoloader provided by Composer to automatically require all classes in third-party packages.
Require the classes from the Twig template engine library using the Composer autoloader instead of calling the Twig one directly.
Use Composer to autoload load any class, not just ones from third-party packages.
Use Composer's autoloader to load all the classes in the framework, replacing the autoload function in the front controller.
Test your knowledge of managing third-party code packages and autoloading using the Composer tool.
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.
Learn what PHP Data Objects (PDO) is and how it makes using databases in PHP much easier by seeing some examples.
Add a model class to the framework, connect to the database in it, retrieve some data and pass it to a view for display.
Learn the best place to connect to the database so that the connection is reused and only used when needed.
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.
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).
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.
Add handlers to the framework to handle both errors and exceptions.
Learn how to view current PHP configuration settings, and how to change them, either in php.ini or in code.
Configure PHP to make sure all errors and exceptions are shown when they occur.
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.
Learn how HTTP uses status codes and how to classify errors in the framework by using them.
Add view templates to display when errors occur in production, based on the error status code.
Test your knowledge of how PHP handles errors and exceptions, and the functionality added to the framework in this section for just that.
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 summary of everything you've achieved on the course, and where to go from here.
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!