Before begining please make sure you understand what MVC is. We will be building a small school application that will evolve as features provided by TaniPHP evolve, so expect new tutorials to appear as new versions are released.
Create a database for our tutorial and edit the taniphp/config.ini.php file to match your settings. Normally you will set your connection settings under the [localhost] section.
For now, we’ll need only one table: the students table.
CREATE TABLE `students` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `school_id` int(10) UNSIGNED NOT NULL DEFAULT '0', `name` varchar(45) DEFAULT NULL, `age` int(10) UNSIGNED DEFAULT NULL, `picture` varchar(45) DEFAULT NULL, `DOB` date NOT NULL DEFAULT '1980-01-01', `created_on` datetime DEFAULT NULL, `updated_on` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `students_FKIndex1` (`school_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Now open your favourite database editor1) and fill the table with some data. Please ignore the course_id field, it will be used the next tutorial.
Now create a model associated with that table. As you know from the conventions page, models are the singular form of the table name, hence our model class name is Student and we will save it under model/student.php.
<?php class Student extends ActiveModel { } ?>
The Controller file is the one that controlls our application. It will get the data from the Model and send it to the View for display.
Since our table is students, we will create a controller class named Students that will be stored in controllers/students_controller.php. This will give us the advantage that upon loading the controller, the framework will also load the Student model and we don’t have to write anything to do that.
<?php class Students extends ActiveController { function index() { $student = new Student; $this->view->set("students", $student->findAll()); } } ?>
The index() method inside the Students controller is called an Action. So when calling http://example.com/index.php?controller=students&action=index, the framework will translate it to a call to index() from the Students controller.
In the index() method we first create a instance of the Student class, then call the $student->findAll()2) that will return an array of Student objects that represent all the records found in the students table. And finally, we assign this array to the View by issuing $this->view->set(...).
The view class will look for a template file to display. The path to the template file is view/controller/action.php. In our case, the controller is Students and the action is index, so the template file used will be views/students/index.php.
<h1>List of students</h1> <table border="1" width="400"> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>-</th> </tr> <?php foreach($students as $s) {?> <tr> <td><?=$s->id?></td> <td><?=$s->name?></td> <td><?=$s->age?></td> <td><a href="?controller=students&action=edit&id=<?=$s->id?>">Edit</a></td> </tr> <? } ?> </table>
Ignoring all the beautifying markup, what this template does is that it cycles through the $students array of objects that we set in the controller, and displays the id, name and age of the student.
The last <td> adds a link to the edit action that we will add in the next tutorial.