Learn MVC by creating your own PHP framework step by step!
MVC pattern, which stands for Model View Controller, is a widely – used programming technology and the most popular technique in web applications development.
The most popular frameworks such as Laravel, Symphony, Yii, Zend are based on MVC pattern. So, it undoubtedly worth learning.
The best way of learning it is creating your own MVC framework and CMS.
Step by step we`ll be creating MVC web application. Further the result of application developing during this course may be put into practice.
As a consequence, we will create a light-weight framework and CMS which may be used in your project. It would contain pages view and editing, contact form, admin section and authorization.
During this course you will learn the best practices in PHP OOP, sessions and MySQL interaction.
If you always wanted to know how modern web applications and frameworks work, this course is definitely for you!
Basic knowledge of PHP OOP is all that you need.
Feel free to ask about free web hosting for this course learning. You can obtain domain, ftp and mysql accounts absolutely free in order to work with this course.
On completing this course you will be a PHP-Academy certified developer.
During this lecture you will create directories of your MVC framework.
Classically, MVC framework consists of a set of directories, which help us organize source code files correctly.
For instance, the source code files of controllers are stored in controllers directory, the source code files of models are stored in corresponding models directory, the views files – in views directory as a consequence. There is no direct access to the content of these directories in a browser, since all the requests to the web-site are redirected to the webroot directory, which we have already created.
The webroot directory stores static content as well as any other files, which may be accessed via direct links. It also contains index.php file – the basic application file, which accepts requests and processes them (creating index.php file and redirecting requests will be implemented in the next lecture).
All the directories will be created in the DocumentRoot directory of your web-server. If you use XAMPP, for example, that sort of directory may be c:\xampp\htdocs, and the access to this directory may be received using http://localhost/address.
In this lecture we will create a mechanism of processing User Friendly URLs. For this purpose:
We will create a special .htaccess file in the DocumentRoot directory, which redirects requests to the webroot directory.
In the webroot directory we will create one more .htaccess file, which redirects requests to the index.php file, if they don`t appear to be real files or directories of the webroot directory.
Taking everything into consideration,
all the requests such as
http://localhost/, http://localhost/pages/, http://localhost/pages/view/about etc are processed by index.php file, which invokes the corresponding controller,
and the requests such as http://localhost/download_me.zip, http://localhost/static.html, http://localhost/css/style.css, http://localhost/test.php etc are processed by web-server according to usual scheme.
In this lecture core classes such as Router and Config will be added. Also, class autoloading will be implemented.
We will add all the functions needed for the URL parsing to the Router class.
Router will be defining language, controller, action and all the other request parameters.
We will also provide a mechanism for creating specific parts of our application such as admin-panel. For this purpose routes will be used. We will create default and admin routes – for the front part of our application and for admin-panel.
All the requests to our web-site will be built (set) in such a way:
/language or route/controller/action/parameter_1/parameter_2/…
If controller, language etc are not given in URL- default values must be used. Hence, we will enforce settings with the help of Config class and set defaults for controller and action, standard route, standard language as well as the list of available languages.
In this lecture we'll add basic Controller class, which appears to be parent class for all application controllers.
We'll also add App class, with static run method in which the object of the Router class will be created , and the corresponding controller will be called.
We'll also create the first real controller in the application which is PagesController, which is in our case responsible for displaying and editing text pages of the site , such as “About Company”, for example.
This is the way the operation algorithm of the application must be by the end of the lecture:
The task of this lecture is showing how controllers work in the base version.
In the next lecture we'll add views to the project.
In this lecture we'll create View class responsible for view rendering.
We'll add corresponding views index.html and view.html for index and view method of Pages controller.
We'll also add view for the default route which will be named as default.html. It contains header and footer, as well as various content according to request. It will be a parent view (template) since it will be used for all the requests to default route, and only content will be changing.
The controllers mission is preparing data for views. HTML code is located in view, and the output of the data which was passed to it.
Rendering is passing data to view and getting the complete HTML code.
Here is the sequence of actions:
In case if we go to http://localhost/admin/pages/index address, the view file would be views/pages/admin_index.html file, and the parent view would be views/admin.html. If we had to output data in the another.html view instead of admin_index.html, it would be enough for controller method to return the path to this view.
Such a principle of file layout and file naming let us work on views easily and simplifies code.
The same principles of view layout in file structure are used in all popular PHP frameworks.
In this lecture we add multi-language support with the help of Lang class and language settings file.
Language settings of each language will be stored in corresponding files in lang directory. As for example: lang/en.php file will be for English, lang/fr.php - for French. Settings will be stored in each of these files as an associative array. As for example, “lng.test” line, will be the array key, and "Sample text" in English and "Exemple de texte" in French will be it's value.
Lang class will have two methods: the first will be used for language settings reading, and the second – for receiving value by certain key. The first method will be called from \App::run function and it also will receive language code as a parameter. The second method will be called only if necessary, in views for example.
We'll also add special global function that is two underscores, for more convenient and quick access to language settings.
We'll check how multi language support works with the help of two examples: http://localhost/en/ and http://localhost/fr/. If language is not specified in URL, standard language, code of which is in settings, will be used.
In this lecture we'll add simple styling of our web site with the help of bootstrap.
We'll also add Contacts controller which is responsible for the Contact Us form.
In this lecture we`ll add some main tables to database of our project.
For example, pages table will have data on text site pages, and users table will have data on it`s users.
In this lecture we`ll need mvc.sql file, in which there are queries to database creating and it`s contents. This file is supplied with this lecture. In order to work with database we`ll need MySQL server and console or such applications as phpMyAdmin or MySQL Workbench.
After creating tables of our database, we add DB class, which will be responsible for connection to it and queries implementing.
We`ll use mysqli in order to work with database
The instance of DB class will be accessible all over the application code via db attribute of the App class.
In this lecture we add basic class for application models – Model class.
In this class we'll create db attribute for convenient access to database.
We'll also add Page model, which will load page or pages list from pages table.
PagesController also will be developed. We`ll add code to it which will use Page model for getting data.
In this lecture we'll add code to the index method of Contacts controller, which will be processing data submitted via contact form.
Data entered by user will be submitted to save function of the Message model. We'll also add the very model in this lecture. The adding of message to database that is to messages table will be implemented In save function.
After message submitting via contact form, user receives flash message that informs him that we have received the message. As for example this flash message may be like: 'Thank you, your message is sent”. Flash messages are short information messages, which may be shown user as a response to any action which is carried out on site.
For this purpose we add code to session class in order to work with flash messages. This is the new class. In the future this class will be responsible for work on sessions and authentication, and it will be developed during the next lectures.
In this lecture we'll create admin panel of our site which will be accessible by http://localhost/admin/.
It will be functioning with the help of admin route the main view of which will be admin.html file.
We'll be extending controllers, models, and adding new views.
Admin panel will let us add, edit and delete new views.
For now admin panel will be accessible without authentication, that is without login and password outputting.
Authentication will be added in the 15th lecture: we`ll allow access only those users who logged in to site using login form and who have admin role.
In this lecture messages list will be added to admin section.
In this lecture we'll add code, which will be responsible for users authentication.
As a result, we may forbid unauthorized users the access to admin panel
We'll also add admin account to users table.
Password will also be equal to admin, but it will be stored in the table as hash for safety, not overtly.
We'll need sessions for users authorization.
We'll also add authorization code to App run method. If user doesn't log in, or it's role isn't equal to admin, it will be redirected to the login page.
Code, which will be responsible for work with session, will be stored in Session class in session.class.php file.
PHP Academy is surely one of the most effective ways for you to acquire all the necessary skills in web development since we truly wish all of our students to become really talented professionals.
We have a great experience in teaching which means that we already have our own magic formula of providing foundation knowledge, so that you will be able to create your own web sites, get the desired job and well-paid position. What is more, we constantly improve ourstrategy of teaching to make our students feel more comfortable, confident and satisfied. Our aim is to deliver valuable knowledge, provide you with pretty simple, succinct, comprehensible and qualitative materials at quite acceptable price. We are absolutely open to our students that`s why you are welcome to ask any questions which we are really thankful for.
You should also notice, that all of our courses have money back guarantee, which lasts 30 days.
We wish you happy learning and successful digesting of our materials!