Write PHP Like a Pro: Build a PHP MVC Framework From ScratchPublisher: UdemyAuthor: Dave HollingworthDuration: 02:56:50Link:https://www.udemy.com/php-mvc-from-scratch/Learn to write faster, easier to maintain, more secure PHP websites by using a model-view-controller frameworkLearn 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. – 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 Introduction 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. The problem with writing web applications: how NOT to structure your code Preview 03:51 Learn what the Model-View-Controller (MVC) pattern is, what each part does, and the advantages of using it. The MVC pattern: What it is and how it can help you write better code 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. Install a web server, database server and PHP on your computer 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. Start writing the framework: Create the folders and configure the web server 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. Create a central entry point to the framework: the front controller 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. Introduction to advanced routing using route variables Preview 01:34 Learn how to go from simple to complex string comparisons using regular expressions, specifically simple character matching and metacharacters. How to do complex string comparisons: an introduction to regular expressions 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 12 Lectures 35:21 Learn what controllers and actions are and how they fit into the framework. Controllers and actions: an introduction 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 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 03:02 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. Views: an introduction 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 errorUncaught 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. Install third-party PHP code libraries automatically using Composer 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. Models: an introduction 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:43 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: Discounts on other courses 00:36