Advertisement

Responsive Advertisement

Creating a Model in RibbonPHP

A model is a class that works to do many backend operation including database and so on. It works to separate the application data and the business logic. Model is where the data are processed with the logic you define.



How to create a model in RibbonPHP?

It is easy. Create a PHP file inside the models directory. The file name and the class name must be similar. For example, if the class name is Account_model, so is the file name. Let's say, Account_model.php. Then, inside the file, you declare the class:

<?php class Account_model{ // methods go here };?>

If you want to work with database in this model, you need to create a __construct() method within this class by adding the following code:

<?php class Account_model{  protected $con; function __construct(){ $this->con = new Database(); //} other methods go here...};?>

By default, RibbonPHP provides you with a dummy model named Home_model. You can open the file and study it.

How to connect the model and the controller?

It is easy. All you need to do is to call $this->model(model-name)->method-name() in your controller. You can see an example in my post about how to create a controller.

Post a Comment

0 Comments