PRESENTATION OF IMPROVEMENTS FOR PHP PROGRAMMERS, BASED ON SYMFONY FRAMEWORK. CREATION OF EXAMPLE PORTAL AND DESCRIPTION OF USED TECHNOLOGY. I.
J. JARMOŁOWICZ, W. ZABIEROWSKI, A. NAPIERALSKI
Abstract – The goal of this work is to present improvements for PHP programmers, brought by Symfony framework. En example auction application wrote in PHP using Symfony will be used as a model for presentation. Conclusions will be based on the experience of the author including the knowledge from books and articles, that he read during the process of creating the application. Keywords: PHP, Symfony, AJAX
II.
INTRODUCTION
Presented technology was picked because of common use of PHP by the internet application programmers. Symfony is young project based on many other mature PHP libraries which were adopted and modified to act as one application. Symfony is complete framework designed to optimize the process of writing the internet application using the most of its capabilities. Presented improvements affect the structure of the code as well as the website interface.
III.
TECHNOLOGY
Symfony framework brings many improvements and standards that raise the quality of application. Here are some of the features: Basic features • Easy installation and configuration on all platforms • Database engine-independent • Simple to use, in most cases, but still flexible enough to adapt to complex cases • Compliant with most web best practices and design patterns • Stable release • Easy to extend, allowing for integration with other vendor libraries Automated Web Project Features Most of the common features of web projects are automated within Symfony, as follows: • The presentation uses templates and layouts that can be built in HTML. Helpers reduce the amount of presentation code to write by encapsulating large portions of code in simple function calls. • Forms support automated validation, and this ensures a good quality of data in the database Jarosław Jarmołowicz, Wojciech Zabierowski, Prof. Andrzej Napieralski -Department of Microelectronics and Computer Science, Technical University of Lodz, Poland. E-mail:
[email protected]
Output escaping protects applications from attacks with corrupted data. • The cache management features reduce the server load. • Authentication and credential features ease the creation of restricted sections and user security management. • Routing and smart URLs make the page address search-engine friendly. • Lists are more user-friendly thanks to automated pagination, sorting, and filtering. • Factories, and plug-ins, provide a high level of extensibility. • Ajax interactions are easy to implement thanks to one-line helpers that encapsulate cross-browsercompatible JavaScript effects. Development Environment and Tools Symfony can be entirely customized. It provides, by default, several development environments and is bundled with multiple tools that automate common softwareengineering tasks: • The code-generation tools are great for prototyping and one-click back-end administration. • The unit and functional testing framework provides the perfect tools to allow test-driven development. • The debug panel accelerates debugging by displaying all the information the developer needs on the page he's working on. • The command-line interface automates application deployment between two servers. • Live configuration changes are possible. • The logging features give administrators full details about how the application is working •
IV.
FUNCTIONALITY
In assumption the auction system should give the user functionality to do two things. First one is to set an auction and the second one is to add an offer to existing auction. The main page of the user application contains three parts: header, sidebar and content. Content presents the results of every action. For the index page, action shows the list of the newest auction, containing few basic information’s about every auction.
Title of every auction is a link to auctions page, which is the action displaying details about the desired auction. Top of the page contains information’s about the price, author and time reaming to finish. Under the table with basic data, there is a list of all criteria’s that offer should comply with. Criteria’s are displayed in groups provided with pictures to increase readability. Below the criteria’s there is a description of auction. Last position is the list of all offers. Right sidebar contains short information’s about the user and a category tree. Both of these elements can disappear regarding on the part of website user is using. Person having an account in the system can place unlimited number of auctions, in every category. Procedure of setting up an auction is composed of several actions. First of all user should pick a category, that auction should be placed in. In the next step portal shows the list of available groups of components, and basket holding the auctions parameters. The basket is understood as a list of components displayed in the frame, including buttons to interact with the content of basket. Groups of components are represented by icons with descriptions, being links to pages with its components lists. Page with components looks exactly like the page with groups, except that icons can be dragged and dropped on the basket area. After dropping an component over the basket popup window appears in which user can add parameters. Parameters are a criteria’s that offer must comply with lined up in few types allowing to compare offers between each other. Every parameter is equipped with weight field allowing to increase or decrease the influence of given parameter on offer value. In other words weight lets differentiate the importance of given parameter. After defining all parameters, user can go to next section allowing to set final settings. User must provide a title of auction, price, description and length of auction in days. Description fields is provided with rich text editor, allowing to add some formatting to text. Date selector is nice looking calendar, open after pressing the button. Set up auction is showed in the list of auctions from the selected category, and also in the list user auctions. Shortened list of auctions contains the auction title, user name, offer count and price interval. Second most important function of the application is to let user participate in auction, by setting their offers in existing auctions. When the authenticated user selects the desired auction, a list of form fields will be displayed to him, allowing type in his offer parameters. Every field must be filled in order to put an offer. Offers are sent to server using AJAX communication, where it is verified. Result of verification is showed as the alert, and depending on verification status new offer is added or error fields are highlighted in form. All offers presented to auction owner have details link, opening popup window with all parameters provided by bidder. Portal is equipped with RSS channels, that allow user to track changes, and new auctions. Second part of the application is designed for administrators to manage the entire project. By default admin application launches the auction module, giving ability to list,
sort, search, edit and delete auctions. The list of items provides links for editing the auction, or sorting results by pressing tale headers. Sorting order may be changed from ascending to descending. List is provided with filter box, allowing to filter result according to given fields. Fields can be filled with search text or select from the list of options. Same construction schema is shared among other auto generated modules like: user list or components list. Completely other approach is presented by category tree module which is implementing drag and drop technique aiming to easy the tree structure modification. Fig. 1 shows the basic structure of category tree, which gives ability to add a leaf on every level of tree. To get to lower levels of tree the
Fig 1. Category tree view
Every leaf or branch can e deleted or moved to other branch To move a leaf You just have to grab it, drag it to other leaf and drop it and then the entire branch will be updated.
V.
ARCHITECTURE
Symfony is based on classic MVC structure (model view controller) marking three layers: • The model represents the information on which the application operates--its business logic. • The view renders the model into a web page suitable for interaction with the user. The controller responds to user actions and invokes changes on the model or view as appropriate. Symfony is going one step forward dividing three basic layers in smaller chunks of code placed in separated files: • Model o Database abstraction o Data access • View o View o Template o Layout • Controller o Front controller o Action
It gives 7 files to render a single web page, but Symfony doesn’t require to write every one of them every time. Most of them is shared and generated automatically, so in practical use only three files must be created: • Action file (action.class.php) • Template file (actionNameSuccess.php) • Layot file (layout.php) – shared for all actions Symfony’s job is to join them and present when action is requested by server.
VI.
AUTHORS SOLUTIONS
Offer comparing system Main task of this application is to help user choose the best offer. In case of a simple auction without parameters the task is simplified, because the best offer is the one with the lowest price. Situation changes when auction has some parameters defined. All criteria values must be treated relative, and the quality of offer will be based on gain of every parameter relative to previous offer. In this approach every parameter has the same influence on the quality of offer independently from the arbitrary value. In addition the weights system influences the result of offer comparison. User creating new auction, can show the most important parameters for him and which one are rather irrelevant, by simply clicking the radiobutton. While the offer is submitted, it’s value is compared to offer that was best until now. Value of offer is calculated by adding the relative growth of every parameter in reference to previous value. In case the value is better (better can mean lower or higher depending on criteria defined by auction owner) than the relative change represented by percentage is added to value of offer. When parameter has a lower value, then the percentage is subtracted from offer final value. Just before every parameter is added or subtracted it’s multiplied by weight of current parameter. The result of summing and subtracting parameters growth is the offers value in reference to the best existing already. If the result is a positive number than it means that offer was better. Negative value indicates that offer was worse. Presented solution of offer comparing isn’t very complicated it’s based on simple math operations, but it tries to follow normal men’s behavior, doing for him work that he would probably do while trying to pick best offer. Dynamic forms Data that comes from forms must be validated before it can be processed and sent to database. In case of form wrote by webmaster and placed in page template data validation is handled by Symfony build in mechanism called Form Validation. Unfortunately this portal handles dynamic forms, generated by users. Form created dynamically can’t be validated by build in mechanism, so one of tasks was to build a data verification system, designed specially for it. Proposed solution combines the server side validation with client side validation. Form containing new offer is sent to server using AJAX call. Symfony provides helper functions that handling the entire operation, so it’s very simple to send AJAX form. On server side the appropriate action is called to verify the integrity and correctness according to criteria’s stored in
database. All fields must be filled, also all fields that should contain numeric values are checked to see if they are really digits. Last part of data verification is to check data ranges for numeric values and lists. The result of data check is stored in array indexed by field names storing error messages for appropriate fields. System checks the size of array after validation. If there were no errors than the value of offer is evaluated. While there are some errors in form the array is coded and send back to client using JSON format. Client browser calls javascript function handling data that came from server. Thanks to Symfony JSON data is decoded and sent to the function transparently. Based on array keys error messages are put near corresponding form fields. Additionally the response status is read so function can display an alert to user and inform him what was the result of offer compare. This approach made it possible to bypass the double data check (on server and than on client side) and preserve advantages of both solutions. So validation on server side prevents corruption of data and possible attack while asynchronous communication and Javascript usage make page delivery faster and produces more readable communication with user.
VII.
CONCLUSIONS
The goal of this work was to create project based on Symfony framework showing improvements for PHP programmers. The task was finished successfully. Presented application works and presents many new futures brought to application development by Symfony framework. Application presents a basic auction portal used by users to find best possible offer among all bidders. Achieved effect is very interesting and future looking. App helps it’s users making suggestions, but also leaving space for them to interact and decide. Thanks to use of good programming practices flowing from Symfony author was able to build application that can easily maintained and developed. Use of complicated graphic effect and Ajax interactions influenced on improving the overall application look and feel, while still being very easy to use by programmers. Symfony has many advantages that ease the life of developers, by making few thing for them or simplifying complicated tasks. When using this framework it’s not required to be a Javascript guru just to add few simple nice looking widgets. Also the process of debugging application is very simplified. Every error and notice is reported all database operations are logged and even the execution time is measured by framework, so developer can get any needed information about page in seconds. Based on experience gathered while working on this project conclusion can be stated that use of Symfony in PHP based web applications improvers the quality of code by structuring and ordering files while adding a new look of usage existing technologies.
VIII.
REFERENCES
[1] F. Zaninotto, F. Potencier “The Definitive Symofny”, Apress 2007
Guide to
[2] P. Hudson, “PHP. Almanach”, Helion S.A. 2006 [3] http://pl.wikipedia.org/wiki/Programowanie_obiektowe, april 2007 [6] K. Repley„RAD or TRAD or both? The future of software development” IEE Colloquium1995 December 1995 pages. 3/1 – 3/2 [7] http://www.yaml.org, april 2007 [8] http://www.symfony-project.com, april 2007 [9] N. Zakas, J. McPeak, J. Fawcett “Ajax. Zaawansowane programowanie” Helion S.A. 2007 [10] J. Coggeshall „PHP5. Księga eksperta”, Helion S.A. 2005 [11] http://www.w3.org/QA/Tips/Doctype, june 2007 [12] R. Dyer, „MySQL. Almanach“ ”, Helion S.A. 2005 [13] R. Bowen “mod_rewrite. Podręcznik administratora”, Helion S.A. 2007
[4] B. Meyer “Programowanie zorientowane obiektowo”, Ksiegarnia S.C. 2005 [5] http://propel.phpdb.org/trac/, april 2007 [14] http://www.w3.org/DOM/, june 2007 [15] http://prototypejs.org/, june 2007 [16] http://script.aculo.us/, june 2007 [17] http://en.wikipedia.org/wiki/RSS_(file_format), july 2007 [18] http://swik.net/Doctrine, july 2007 [19] http://www.eclipse.org/pdt/, july 2007 [20] http://www.eclipse.org, july 2007 [21] http://pl.wikipedia.org/wiki/SQL_injection, july 2007 [22] http://www.mysql.org/, july 2007 [23] http://www.innodb.com/, july 2007