As I explained in the segment that you just read, the user controller class implements two methods that render HTML on screen by means of two view files. The first one is “index(),” which is tasked with displaying all of the users stored in the corresponding MySQL table, while the second one is “create(),” which is responsible for outputting an HTML form for entering data about a new user. In accordance with this, I’m going to create the file that displays user data on screen, which will be called “users.php.” The definition of this file is as follows: (users.php) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <?php if (empty($users)):?> <h2>Oops! There are not users in the database.</h2> <?php else:?> <?php foreach($users as $user):?> <p><strong>First Name : </strong><?php echo $user->fname;?></p> <p><strong>Last Name : </strong><?php echo $user->lname;?></p> <p><strong>Email : </strong><?php echo $user->email;?></p> <hr /> <?php endforeach?> <?php endif?> </body> </html> Nothing really spectacular, right? As you can see, this view file that displays the list of users stored in the MySQL table is simply a mixture of HTML and interspersed PHP code, which in this case comes in handy for traversing a result set and showing information about each user. This file, along with the other one that I’m about to show you, put in evidence the need for adding to the framework a view helper that sanitizes output, in order to prevent XSS attacks. This will be left as homework for you. Now, it’s time to list the view file that creates the HTML form used for inserting new users into the database. This one is called “user_form.php,” and looks like this: (user_form.php) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <?php echo Form::open(array('action' => 'save', 'method' => 'post'));?> <p>First Name: <?php echo Form::input(array('type' => 'text', 'name' => 'fname'));?></p> <p>Last Name: <?php echo Form::input(array('type' => 'text', 'name' => 'lname'));?></p> <p>Email: <?php echo Form::input(array('type' => 'text', 'name' => 'email'));?></p> <p><?php echo Form::input(array('type' => 'submit', 'name' => 'send', 'value' => 'Create user'));?></p> <?php echo Form::close();?> </body> </html> Definitely, this file is slightly more interesting than the previous one, as it uses the “Form” helper class that you saw in previous tutorials for rendering the different parts of the form that insert a new user in the MySQL table. Also, notice that the “action” attribute of the web form points to the “save()” method in the controller, which simply takes the collected data and inserts it directly into the database table. Now that the two view files used by the controller class have been properly defined, you can try this sample application. So, say that you want to list all of the users stored in the database. To do that (assuming that the framework’s components and the application’s classes reside in a folder called “mvc” on the web server), you should type the following URL into your browser: http://localhost/mvc/users If all goes well, you will see the lists of users displayed on the browser. On the other hand, if you wish to add a new user to the database, you should enter the following: http://localhost/mvc/users/create It's simple to read and SEO-friendly. Naturally, under normal conditions, direct calls to controllers and methods should be done through regular HTML links, but remember that this is only an illustrative example and nothing else. Anyway, adding those links would be as simple as including them in the appropriate view files. Well, at this point, this sample MySQL-driven application is working as expected, thanks to the proper use of the classes provided by the framework. Thus, I’m going to end this tutorial of the series by showing in one single place all of these recently-created files, so you can study them in more detail. Now, go ahead and read the following section. We’re almost finished here!
blog comments powered by Disqus |
|
|
|
|
|
|
|