Before we start coding the view files required by the user controller class created in the previous tutorial, it’d be useful to recall the definition of this component of the framework. This way, you can see how easy it is to build an application that sticks to the MVC design pattern. The file containing the definition of the user controller mentioned in the introduction was built like this: (UsersController.php) <?php class UsersController { private $model = NULL;
// constructor public function __construct() { // store model object as property $this->model = new Model(MySQL::getInstance(array('host', 'user', 'password', 'database'))); } // fetch and display all users public function index() { // create view object $view = new View('users'); // create view properties $view->title = 'Using the MVC design pattern with PHP 5'; $view->heading = 'User List'; $view->users = $this->model->fetchAll(); // display view echo $view->display(); } // create new user public function create() { // create view object $view = new View('user_form'); // create view properties $view->title = 'Using the MVC design pattern with PHP 5'; $view->heading = 'Create new user'; // display view echo $view->display(); }
// save user public function save() { // get POST params if (Input::post('send')) { $fname = Input::post('fname'); $lname = Input::post('lname'); $email = Input::post('email'); // save user data $this->model->save(array('fname' => $fname, 'lname' => $lname, 'email' => $email)); } } // update existing user public function update($id) { $this->model->save(array('fname' => 'My first name', 'lname' => 'My last name', 'email' => 'myemail@domain.com'), (int)$id); } // delete existing user public function delete($id) { $this->model->delete((int)$id); } }// End UsersController class As you can see, the user controller class is comprised of a few straightforward methods that allow you to retrieve, insert, update and delete records on a MySQL table populated with data on some fictitious users via the model’s API, including their first and last names, and their email addresses as well. Of course, due to the inherent flexibility offered by the model, switching between different database tables is a breeze, but for the illustrative purposes of this tutorial I’ll keep using the table that stores only user data. So far, so good, right? At this stage, you understand the underlying logic of the previous controller class, which also makes use of other components of the framework, such as the input and view classes, to do its business. However, the instantiation of view objects within the controller implies a direct inclusion of view files that output something to the browser. In this case, the process occurs in the “index()” and “create()” methods respectively. Obviously, it’s necessary to define these files to get this sample PHP application working as expected, so in the following section I’m going to do precisely that. Now, to learn more about this process, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|