AL 1 MA TE RI Introducing Symfony, CakePHP, and Zend Framework An invasion of armies can be resisted, but not an idea whose time has come. GH WHAT’S IN THIS CHAPTER? TE D — Victor Hugo General discussion on frameworks. ‰ Introducing popular PHP frameworks. ‰ Design patterns. RI ‰ CO PY Everyone knows that all web applications have some things in common. They have users who can register, log in, and interact.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK WHAT ARE WEB APPLICATION FRAMEWORKS AND HOW ARE THEY USED? A web application framework is a bunch of source code organized into a certain architecture that can be used for rapid development of web applications. You can think of frameworks as halfproduced applications that you can extend and form to make them take shape according to your needs.
WHAT ARE WEB APPLICATION FRAMEWORKS AND HOW ARE THEY USED? x 3 is nothing wrong with a piece of code being a library, as it is just a different entity. And there are also some bad frameworks that damage the reputation of the good ones — basically you can take any half-done application, release it, and call it a framework. These two software groups just behave differently and should not be confused.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK ‰ For producing consecutive apps, in which modularity and reusability of pieces of code like controllers and views may be helpful ‰ For real-world development with deadlines, rotating staff, and fitful customers ‰ If you are, or want to be, a professional web developer, so learning how to work with frameworks is not an excessive effort As you can see, this applies to most commercial web applications that connect to a database and allow
WHAT ARE WEB APPLICATION FRAMEWORKS AND HOW ARE THEY USED? x 5 technologies. Then in 2005 there was a boom of Ruby. Everyone was amazed with the elegance of this programming language; and Ruby on Rails, the central piece of software ever written in Ruby, was claimed to be the ultimate web applications framework. Soon clones of Ruby on Rails began popping out. That’s how Python’s Django and Turbogears, as well as all PHP frameworks were born. In 2004 PHP5 was released. It was neat and object-oriented.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK OPEN SOURCE PHP WEB FRAMEWORKS Another question we want to answer is why we have chosen these three particular frameworks.
OPEN SOURCE PHP WEB FRAMEWORKS FIGURE 1-2: Comparison of search volumes of different PHP frameworks The First Look The fi rst look at the frameworks really gives us little information on their individual features. Their websites just try to impress you with marketing descriptions and a list of features that vary little from one framework to another: “Symfony is a full-stack framework, a library of cohesive classes written in PHP.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK tested agile codebase. Zend Framework is focused on building more secure, reliable, and modern Web 2.0 applications & web services.” Now see whether you can spot three differences. Well, the websites are not really informative about unique features of their frameworks. You can fi nd more in various blogs and forums, but still there is little verified data, and general discussions tend to exchange purely personal opinions.
OPEN SOURCE PHP WEB FRAMEWORKS x 9 CakePHP was started in 2005 by the effort of Polish web developer Michał Tatarynowicz. Heavily inspired by Ruby on Rails, CakePHP is an entirely community-driven open source project with lead developer Larry Masters (aka PhpNut). The next major release of CakePHP has also been announced, but its release date is still unknown. The most important goals of CakePHP are its friendliness, development speed, and ease of use. And it really excels in that.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK disproportionally huge and perhaps somewhat excessive. Here is an overview of a few more notable ones that we have found to be used successfully to develop web applications. CodeIgniter Started: 2006 License: modified BSD PHP versions: 4.3.2+ Its logo is shown in Figure 1-6. Website: http://codeigniter.com CodeIgniter is developed and maintained by a privately-owned software development company, Ellis Labs.
OPEN SOURCE PHP WEB FRAMEWORKS x 11 Agavi Started: 2005 License: LGPL PHP versions: 5.2.0+ (recommended 5.2.8+) Its logo is shown in Figure 1-8. Website: www.agavi.org Like Symfony, Agavi is based on the Mojavi framework. It was started in 2005, but the 1.0.0 version was worked upon until early 2009. The source code is very polished and sometimes called the best-written MVC OOP framework. However, it has not gained much popularity, perhaps due to scarce documentation.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK Yii Started: 2008 License: BSD PHP versions: 5.1.0+ Its logo is shown in Figure 1-11. Website: www.yiiframework.com FIGURE 1-11: Yii logo Yii was founded by a developer of Prado and it continues many of its conventions. Yii is very fast (leading in most benchmarks) and extensible, modular, and strictly object oriented. It has a rich set of features and decent documentation.
OPEN SOURCE PHP WEB FRAMEWORKS x 13 Qcodo Started: 2005 License: MIT PHP versions: 5.x FIGURE 1-14: Qcodo logo Its logo is shown in Figure 1-14. Website: www.qcodo.com Qcodo is an MVC framework that excels in code generation from database design. It has a very powerful code generator that analyzes the structure of the data model, and creates PHP object code and also HTML pages for database manipulation.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK DESIGN PATTERNS IN WEB FRAMEWORKS There are certain abstractions that can be transported between applications in order to make the development process faster. This section takes a closer look at these abstractions and the way they shape the web application frameworks.
DESIGN PATTERNS IN WEB FRAMEWORKS x 15 ‰ Model — Represents the business logic of the application. It is more than just the raw data; the Model has to represent the structure of data with all relationships and dependencies. It may comprise one or more classes that correspond to logic objects of the application and provide an interface for manipulating them. The Model is the only layer that uses persistent storage. It should completely encapsulate all database connections.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK conferred by the authors of the frameworks. So we will call all frameworks Model-View-Controller, even if the Controller does the majority of data-transferring work. Data Manipulation and Delivery Action Presenter Updates Model View Result FIGURE 1-18: Model-View-Presenter pattern Overview of Other Design Patterns Design patterns can be divided into creational, behavioral, and structural patterns.
DESIGN PATTERNS IN WEB FRAMEWORKS x 17 private static $isRented = FALSE; private function __construct() { } static function rentCar() { if (FALSE == self::$isRented ) { if (NULL == self::$car) { self::$car= new CarSingleton(); } self::$isRented = TRUE; return self::$car; } else { return NULL; } } function returnCar(CarSingleton $carReturned) { self::$isRented = FALSE; } function getMake() {return $this->make;} function getModel() {return $this->model;} function getMakeAndModel() {return $this->getMake().
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK } else { $this->drivesCar = TRUE; } } function returnCar() { $this->rentedCar->returnCar($this->rentedCar); } function getMakeAndModel() { if (TRUE == $this->drivesCar ) { return ‘I drive ‘.$this->rentedCar->getMakeAndModel().’ really fast!’; } else { return “I can’t rent this car.”; } } } ?> code snippet /singleton/Customer.class.php We can test these classes with the following code.
DESIGN PATTERNS IN WEB FRAMEWORKS x 19 Customer_2 wants to rent the car. Again. Customer_2 says: I drive Dodge Magnum really fast! The Singleton pattern is used often in other design patterns such as Prototype, State, Abstract Factory, or Facade. Apart from that, it can be used in all classes where you need a single instance with global access, but there is no way to assign it to another object, and perhaps you can also benefit from initialization on the fi rst use.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK model; } function getColor() { return $this->color; } function setColor($colorIn) { $this->color= $colorIn; } } class DodgeCarPrototype extends CarPrototype { function __construct() { $this->model = ‘Dodge Magnum’; } function __clone() { } } class SubaruCarPrototype extends CarPrototype { function __constru
DESIGN PATTERNS IN WEB FRAMEWORKS x 21 echo ‘Fine, we will paint your ‘.$customersCar->getModel(). ‘ ‘.$customersCar->getColor().’.
’; ?> code snippet /prototype/Test.php The previous code will result in the following messages: Which car do you want? Subaru Outback. What color do you want? Fine, we will paint your Subaru Outback red. The Prototype pattern is used commonly in different modules of frameworks.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK decorated object and extends its responsibilities dynamically. It is like putting a gift into a solid box and then wrapping it with colorful paper — it is still a gift, but durable and decorated. The inheritance structure of the Decorator pattern is presented in Figure 1-22.
DESIGN PATTERNS IN WEB FRAMEWORKS x 23 car = $car_in; $this->comfortMessage = $car_in->comfortMessage; } function drive() { return ‘Accelerating. ‘ . $this->car->gearMessage . ‘ Driving comfort is ‘ .
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK echo ‘Driving the car without decoration:
’; echo $car->drive() . ‘
’; ?> code snippet /decorator/Test.php And the result will be the following: Driving standard car: Accelerating. Remember to shift up. Driving comfort is standard. Driving fully decorated car: Accelerating. Auto transmission shifts up. Driving comfort is very high. Driving the car without decoration: Accelerating. Auto transmission shifts up.
DESIGN PATTERNS IN WEB FRAMEWORKS x 25 So, how do you create such Chain of Responsibility? The main idea of this pattern is to process a request by a list of consecutive handlers to avoid any hard-wired mappings. The initial client holds a reference only to the fi rst element in the chain of handlers. Then each handler holds a reference to the handler afterward. The last handler must always accept the request to avoid passing it to a NULL value.
x CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK application may display different pages depending on which steps are needed to complete the transaction. State Context +request() state.handle() +handle() ConcreteStateA +handle() ConcreteStateB +handle() FIGURE 1-25: State pattern structure Iterator There are many kinds of aggregate objects and many ways to traverse them. One simple example is an array traversed by consecutive integers supplied to the array operator.
DESIGN PATTERNS IN WEB FRAMEWORKS x 27 of traversal can be produced for both of them. When Client calls the next() method, different ordering algorithms are executed for List and for Map, but in both cases a subsequent element is found.