Creating a PHP website using MVC – 4 Creating Model class

The Model class we create is the base class for all Models and it contains some common functions like getAll(), getOne(), getById(), search(), count() etc. These static methods will also be available for all other Models extending the base Model class. The model creates instances of itself for each result item for the query. i.e., if we call Book::getAll() we will get an array consisting of Book instances.

We might need to define item specific methods and properties into the classes though. In any of the inherited model classes don’t need to call any database calls directly. The model base class does this behind. When we call the method save() on an instance, it will get updated in the database and if it is not found in the database an entry is created.
Continue reading

Creating a PHP website using MVC – 3 Creating a Database Wrapper

In the previous parts of this tutorial series, we learned how to create a working example of an MVC framework, but without a Model. Now we will create a Database wrapper singleton class which can be used everywhere.

Before we create a Database wrapper, let us create an install file which installs required database, tables etc. Create a new folder in the public folder called ‘install’ and create an index.php inside. Copy this code into the file.
Continue reading

Creating a PHP website using MVC – 2 Creating the Controller and Template

In the first part of this tutorial series, we looked into how to startup the framework.  The output was displaying the controller and action of a called url. We also found which area is called – the admin or the site area.

In this tutorial, we will look into creating the Controller and Template classes.

For any class we create anywhere, we need to make sure that the filename is ended with ‘.class.php’ because this is how we defined autoloader and a class can be easily identified by just looking into the file system.
Continue reading

Creating a PHP website using MVC – 1

This is the first part of php mvc website design tutorial series. You already know what is MVC architecture and you might even know how to implement it. There is already a debate about the design of an mvc architecture. I am not going into that. The truth is whatever you think is easy, follow it. If there is Model, Controller and Views in your php application and have a consistent pattern around all things, you can suppose it to be in an MVC pattern.

Continue reading