A Web App Based on a Model for the CodeIgniter PHP Framework - The property model class in action
(Page 4 of 4 )
As I expressed in the previous segment, it’s necessary to define a controller that takes advantage of the functionality provided by the “Property_model” class to fetch, save and delete properties in the corresponding database table. This is how we will construct our simple real estate web program.
Basically, the signature of this properties controller class looks like this:
In this case, a single controller class is all that I need to implement a basic CMS system that handles properties for a real estate application. As you can see, the controller’s constructor simply loads the “Property_model” class, while the “index()” method uses this model to list all of the properties available in the “properties” MySQL table.
On the other hand, the “save(),” “update()” and “delete()” methods, as their names suggest, are used to insert, update and delete properties in the corresponding table. Besides, it’s worthwhile to note that each of these methods will load an error view if a failure occurs, which helps to code the application in a more defensive way. And finally, to complete the previous example, here are the view files used to display properties and errors respectively:
(‘properties_view.php’ file)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<title><?php echo 'An error was found!';?></title>
</head>
<body>
<h1><?php echo 'An error was found!';?></h1>
<p><?php echo $error;?></p>
</body>
</html>
Now that I have shown you the two view files that display errors and property information, the development of this sample real estate web application is complete. Finally, if you wish to try out this program with your own CodeIgniter installation, don’t forget to load the database class either in the controller or automatically to get the program working as expected.
Final thoughts
It’s hard to believe, but we’ve come to the end of this hopefully educational journey. Overall the experience has been fun and instructive too, since in different tutorials you learned how to build an abstract model class for the CodeIgniter PHP framework with minor efforts. From this point on you can either pick up more complete model libraries like DataMapper and IgnitedRecord, or you can use the previous sample model class and introduce your own enhancements according to your specific requirements.
See you in the next PHP development tutorial!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.