Create php MVC application. Configuration files and creating routing file

Главная » Tutorials » PHP+MySQL » Create php MVC application. Configuration files and creating routing file

Today we will begin to write code of our application. We’ll write configuration files, database connection class and routing class. Also we’ll create parent controller, model and view classes and controller and template for app main page.

First of all we need to start from index.php file. This file only includes config file (see index.php code).

In the config file we’ll define common constants and include common files of our app (model, controller, view, database connection class and routing) and run method for define route (URL) of application (see conf.php).

Now we need to write class for database connection. This class will be static. For connection establish I will use PDO.

Now let’s talk about routing. As already mentioned, we create MVC app. Of course we needn’t to create folders and files for every section and page – it is absolutely wrong and unprofessionally. This task routing will do. In fact, when we watch web site pages, we ask it to do some task – give user list, open form and so on. Furthermore we can get users list and products list in the same page. In our application page will be controller and task will be action. Controller is class, and actions are methods of this class.
First of all we’ll define default controller and action – indexController, and index action. In other situations we will get a part of address, using $_SERVER[‘REQUEST_URI’] and aplit it into two parts. For example, /cabinet/users url implies including CabinetController and running users method.
In our routing file there were defined some rules for files naming. Files must start from capital letter with Controller prefix. For example IndexController, UserController and so on.

After this we need to create common parent controller class, model class and view class (see Controller.php, Model.php, View.php).

Now we need to write code for IndexController and create view. For view (template for form authorization) I will use this template. We’ll define index action in IndexController, which calls render method of View class. It draw html in browser, accepting two parameters – html template and array with dynamic page data – page title, messages and so on. IndexModel we will implements in next lesson.

Подписаться на рассылку

Будьте в курсе - получайте последние статьи на свой email

Ваша подписка успешно оформлена

Pin It on Pinterest

Share This