Thursday, June 14, 2007

Organizing Web Applications

Putting Order To Your Code...

In my experience with coding and fellow programmers, I have noticed the hardest part of integrating with each other is coding standards. I don’t just mean writing the endless lines of code. In this article I want to discuss organizing the files and common practice principles that can help others decipher your code. This is by no means the right way or the best way, its merely a way. I have developed many of these practices through time and experience so these are suggestions. This is written from the perspective of PHP and MySQL web applications.


I will break this down into navigation and the management of an entity. Navigation will be where the visitor is and where they can go. Management of an entity is handled with a class that performs tasks on the tables associated with that entity. This is common in applications such as ecommerce and information management sites.


Navigation:

In my sites and applications navigation is controlled by a driver. The drivers are located at the various levels of the application and decide the path of travel. They interpret the navigation parameters and direct the visitor to their destination. The use of navigation levels correlate to the menu items. Top level menus are referred to with nav1, submenus are referred to with nav2 and so forth. This allows infinite number of navigation in menus and submenus. It will not confuse actions with navigation. A crumb trail is available using this method and iterating the navigation variable. A nav1 driver will be at the top level while a nav2 driver may be at a lower level such as a customer interface for managing customers. This would include the submenu and logic to determine the controller to use. The Controller will be covered later.


Actions:

Actions are different than navigation because they tell the controller what to do. Possible actions are add, edit, delete, list, select. The controller will perform the specific action using a model for that entity, may set a message variable, and then display the proper view.


Models:

These are not as sexy as the ones in the magazines but may be more important. I generally use a class or several classes to manage an entity. Most of the time I have a class for each table in my database and that class will add new records, edit or save changes to a record, or even delete records. I write a few public methods to return lists for pages as well.


Controllers:

Controllers do the dirty work. They instantiate the classes and execute the methods based on the action defined. The are even in charge of displaying the proper file before or after the action is performed. For instance, by default most times my controller will point to a page with a table list of records. Once you click on a delete or edit button I may either delete the record, display an "Are you sure?" prompt, or display an edit form filled in with the current values and then save those changes with an "Apply Changes" action. Our action may tell us to make a new record, I show the new record form and if my action is "Save" then I write that new record to the database. The controller may even call functions to validate data. As you can see, it’s a central location for much of the control logic and operations of the application.


Views:

Finally I want to talk about views. I use these as basic display files. They are the forms, table lists, and general html pages with a little dynamic content mixed in. Keeping these isolated has allowed me to turn over the entire views directory to a CSS master and those are the only files they need to edit. Since there is not much code and mostly html they are easy for almost any web developer or layout specialist to manage.


So that’s it. Hopefully this was helpful and you can start to organize your code in a way that can be passed on to others. Most of the inspiration was from my limited experience with CakePHP. I did enjoy their framework but many of my applications have extensive custom coding and have been easier to work with outside of a full framework. I found the need for standards more than a completed framework. Feel free to elaborate more on these suggestions. I am always innovative and inquisitive at the same time. Now get back to work.





Brian J. has been involved in web design since 1997. He is the founder of True Vision Computer Services, Inc. His recent focus has been on web applications and information systems development.

No comments: