Zend, symfony, CakePHP
A few years ago, one of the most common criticisms of PHP was that it did not support the MVC-style architecture. With the changing times, there are now many options available in the PHP framework field. This series will introduce three widely used PHP frameworks-Zend, symfony, and CakePHP-to verify the similarities and differences.
The "php framework" series is designed for PHP developers who want to get started with the framework but have no chance to detail the available framework. After learning this series, you will understand the reasons for choosing these three frameworks, how to install each framework, and make full use of the testing applications that will be extended in the three frameworks. It sounds like you have a lot to learn, but don't worry. Although there are many contents, we have subdivided the content into multiple manageable parts.
About this series
Part 1 of this series will list the content covered in this series, introduce the framework to be tested, explain how to install it, and view the first test application to be built.
Part 1 will guide you through building sample applications in three frameworks, highlighting their similarities and differences.
Part 1 starts from the extended test application and then handles exceptions that do not conform to general rules. All frameworks can well complete their tasks. In each project, you need to complete some things beyond the framework setting. The first section describes the situations.
Part 1 describes Ajax support. Use the local code and third-party libraries to verify the use of Ajax-specifically describes how each framework runs and accepts specific frequently-used libraries.
Part 1 will address how to work outside the framework. Set a task (update the script every night) and verify the process of completing this task in each framework.
This article provides a lot of content. You must keep up with the pace quickly. This article will examine the three frameworks. What are the three frameworks? Why are these three frameworks targeted? What other frameworks are there? Why not select other frameworks? Too many questions to answer! However, most questions will be answered before the installation process of each framework is executed. Then you will learn about the testing applications used in subsequent articles of this series. You can immediately learn about the differences between different frameworks and what the same work can be achieved by these frameworks.
System Requirements
Before you start, you must have a certain working environment. The following requirements must be met:
HTTP server that supports sessions (and mod_rewrite is recommended) -- this series uses Apache v1.3 with mod_rewrite enabled.
PHP v5.1.4 or later-not all frameworks require PHP of this version, but for simplicity, all frameworks will be installed using the same PHP. This series uses PHP v5.2.3.
Recently released MySQL installation-you can use and support several other database options, but this series and sample applications are all written in MySQL v5.0.37.
Supports PHP v5.2.4 or later versions and MySQL operating systems-Linux is used in this series. If you are a Windows user, you need to make obvious adjustments, especially when creating a directory.
Prerequisites
Assume that you have an understanding of PHP, application design, and how to work in the database. Do not require your experience in using the framework, but you should be ready to start using the framework.
Model Framework
Before you know which frameworks are selected, it is necessary to discuss what frameworks are.
The framework idea is to provide a design that can be used in multiple applications. All applications have a lot of common basic content-specifically, some database interfaces, some application logic, and the content that applications present to users. If you have compiled many PHP applications, you will know what the basic content is. You may have written a group of functions, read data from the database, and write data to the database. You may have used template engines such as smarty to manage the UI. A large amount of PHP code must have been written to complete transactions such as analysis form submission and decision based on submitted data. If you have already compiled many applications, you may have done the same basic work again and again, and sometimes borrow the code from one application to another.
The framework is designed to provide structures for these common elements (Database interaction, presentation layer, and application logic), so that you can spend less time writing database interface code or presentation layer interfaces, it takes more time to write the application itself. Applications are decomposed in this way. This architecture is called Model-View-controller (MVC ). Model index data, view refers to the presentation layer, and controller refers to the application logic or business logic. The complete MVC discussion is beyond the scope of this article, but we encourage you to study MVC and learn more about MVC (see references ).
Select framework
Almost every language has several frameworks available. It may be difficult to select a framework that meets your needs, especially when you have never used any framework before. Although it is helpful to be familiar with the suggestions and comments provided by colleagues and reliable developerworks authors in this field, the only guiding principle that should be followed when selecting any framework is: the more time and effort the framework saves, the better. If a framework works well but produces a large number of support calls, it is not a good framework. If a framework is easy to support but does not impede development, it is not a good framework. If a framework is very elegant, but it may lead to support and development problems, this framework is useless.
When selecting a project framework, consider the opinions of all relevant personnel from top to bottom, and consider the impact on other parties when evaluating the framework.
When you consider using a framework, further examine your application and consider whether the application requires a framework. The framework is not a necessity. You can write enterprise applications even if you do not use the framework. Will the framework be helpful to the project? Does it save everyone's time and energy? Will your application be better executed in the framework? Will it provide the lack of stability? If the answer to any of the above questions is yes, you should consider using a framework. If the answer to all these questions is no, the use of the framework will only complicate things.
Unfortunately, the length and scope of this article cannot fully introduce all available PHP frameworks. This series mainly introduces three frameworks:
Zend framework
Symfony
CakePHP
There are many reasons for choosing these frameworks, but they may all fall into these categories: "The framework you have heard of", "frameworks already installed by some people", or "frameworks you have talked about ". I encourage you to study codeigniter, Seagull, Web Application Component Toolkit (Wact), Prado, zoop, PHP on Trax, or one of many other available PHP frameworks. The framework selection depends on your personal preferences, just like the language used to write code. This series does not tell you which framework is better or worse than other frameworks. If a framework is well performed in a certain aspect, we will discuss it. Which framework does not seem practical? We will also discuss it. Even if we cannot fully cover various frameworks, our approach will help you learn to measure the advantages and disadvantages of other frameworks. You need to have your own opinion on the framework being tested, which framework you like, and decide which framework you will continue to use.
Zend framework
Everyone knows Zend-"php company ". When you download and install PHP, from around V3, you are downloading PHP from Zend. In addition to the release of PHP, Zend technologies has provided a large number of PHP support technologies over the years. Zend provides a framework suitable for PHP. This is a popular framework that has been downloaded 2 million times so far. If your boss has heard of the PHP framework, it is likely to be referring to the Zend framework.
Symfony
Symfony, sponsored by sensio, aims to speed up the creation and maintenance of web applications and replace repetitive coding tasks with functionality, control, and pleasure ". The symfony framework has been used in many enterprise applications around the world, probably including the most famous askeet and Yahoo! Bookmarks. If someone you know installs, uses, or plays with a PHP framework, it is probably symfony.
CakePHP
CakePHP is mostly derived from Ruby on Rails and aims to introduce simplicity and scalability into the PHP framework. CakePHP has always been regarded as a top-level PHP framework, and PHP V5 was recently selected as the core of mambo content management system. Driven by powerful communities and rapidly growing user libraries, CakePHP's popularity is steadily increasing. If you have heard about the PHP framework, the discussion may be about CakePHP.
Install
Each framework has its own set of installation instructions, and the complete installation details are beyond the scope of this article. This article will focus on the installation of these three frameworks, highlighting the points that may not be involved in the installation instructions for each individual framework, or the content that should be different from the installation instructions, it is very special to install all three frameworks on one computer.
First, create a folder to save all the code and framework and include the content required by this series (for example, the directory named/column ). In this directory, create four additional directories: htdocs, protected, include, and SRC. Edit the Apache CONF file so that the root directory of the file points to/column/htdocs -- enable mod_rewrite when you are in this directory (if not already in this directory. Edit the php. ini CONF file to include the/column/include/directory in the include_path command. If you do not understand the meaning of all these operations, please do not worry. Everything becomes clearer when you install the framework.
Install the Zend framework
Download the Zend framework (see references ). This series uses v1.0.1. Download and decompress the Zend framework to the/column/src directory. The release contains several text files and three directories: Demos, tests, and library. Copy the contents of the library directory (which should be a folder named Zend) to the/column/include/directory. Now there should be several directories and files named/column/include/Zend. These are the libraries used by the Zend framework.
Okay! You have installed the Zend framework.
Install symfony
Download symfony (see references ). You can install symfony in any of the methods listed on the download page, but we recommend you download the tarball for our purposes. Decompress the tarball to the/column/src/symfony/directory. You should see four directories (data, Doc, Lib, and licenses) and some readme documents. Not all of these documents and directories are required for the installed applications. However, these documents and directories are available now. The installation may seem too simple. Remember that these frameworks are generally composed of utilities and libraries, so it is not difficult to install the framework. Some append configurations are required to use the framework. You will know how to configure it later.
Install CakePHP
Download CakePHP (see references ). This series uses version 1.2.0.5427alpha. Whether it is a mature alpha version or not, the 1.2 branch of CakePHP has become a widely accepted standard in the CakePHP community. Download and decompress the tarball and put its content in/column/src/CakePHP. Four directories (app, cake, Doc, and vendors) and two files (. htaccess and index. php) should be obtained ). Some files in these files will be operated later, but now we can.
Test the application
You have downloaded and installed three frameworks. However, you may have noticed that you have not used these three frameworks to perform any actual operations, such as using the Hello world application to verify the installation. That's because you have to skip Hello world and go to more in-depth (but not very in-depth) content.
Building a hello World application in the PHP framework may be a bit painful. Framework usage requires a certain amount of system overhead, and this overhead occurs when you try to create simple content such as Hello world. However, when dealing with more complex applications, the same system overhead seems to be reasonable.
The test application you will build is not complex. This test application allows you to paste text into the application using the text area. The text will be saved to the database and displayed by the application during request retrieval. With such an application, you can easily create and update content on the web site without editing HTML or uploading new files. This highly innovative application that uses new methods will be called blabhg.
How to Use blahg?
According to the description of blahg, you may have inferred several considerations for designing an application. Blahg requires four elements:
Page containing the form-This form must contain at least one text area. This page will be written to the page.
The page that reads the pasted content-This page will be passed to a post identifier. This page reads the page.
List pages with available pasted content-This page will be an index page.
Database Table for saving the pasted content-This table must at least save the pasted text and post identifier, and may also save the post title and the last modification date of the pasted content.
It is obvious that blahg can be more robust than today, and you will perform some enhancement work later in this series. But now, meeting these simple requirements is enough. Unfortunately, you are not yet given the opportunity to build Hg in any framework. That must be done in Part 1. But now is the best time to set the database you will use.
Set Database
You need to set up the database before you can actually build the blahg. Not every single framework must set a database, but for the purpose of this series of learning, this will make things more orderly and avoid the necessary table prefix.
Create three databases named Zend, symfony, and cake, and grant all permissions for each database to the corresponding users. The commands of each framework below will contain the SQL required to create the posts table.
While playing with the database, you can also create a table that will be used at the beginning of blabhg. Use the following SQL statement to create a posts table in the Zend and symfony databases.
Listing 1. Create a posts Table in Zend and symfony
Create Table 'posts '(
'Id' int (10) Not null auto_increment primary key,
'Title' varchar (255) not null,
'Text' text not null,
'Modified' timestamp on update current_timestamp not null default current_timestamp
) Engine = MyISAM;
For Zend and symfony, maintenance of modified columns is left to the database. CakePHP uses slightly different SQL statements. This will make cake a magic (especially, CakePHP will automatically maintain the modified columns ).
Listing 2. Create a posts Table in CakePHP
Create Table 'posts '(
'Id' int (10) Not null auto_increment primary key,
'Title' varchar (255) not null,
'Text' text not null,
'Modified' datetime default null
) Engine = MyISAM;
This is as simple as using Table syntax. Depending on the appearance of these tables, you should be very clear about the appearance and running of blabhg. You may even build blahg yourself.
Next step
Please resist the temptation to immediately invest in and start to play with the Framework. Instead, it takes some time to build blahg without using any framework. Write an application in your usual way, regardless of what method it is. Be sure to keep up with the requirements listed above when building blahg-do not be too imaginative. By building a test application outside the framework context and modifying the application to include enhancements built in this series, you can well measure the degree to which a given framework meets requirements and development styles.
Back to Top
Conclusion
Here, we have laid the foundation. I hope your installation can be completed smoothly, but if you encounter any problems, you must study the installation documentation for each separate framework (see references ). Please take some time to build blahg based on the above instructions. It is worth your time.
References
Learning
For more information, see the original article on the developerworks global site.
See the Zend framework manual.
Obtain the symfony document.
Refer to the CakePHP Manual (Note: This manual was written for CakePHP V1.1. If you are using CakePHP v1.2, there may be some differences ).
Read the five-part series of tutorials "cook up web sites fast with CakePHP ".
Visit Wikipedia to learn more about the software framework.
Get an overview of the MVC structure from Wikipedia.
For more information, see the PHP manual.
For other PHP documents, visit php.net.
For more information about how to use PHP programming, see the "Learning PHP" series on developerworks.
Php.net is an important resource for PHP developers.
View the "php recommended books list ".
Browse all PHP articles and PHP tutorials on developerworks.
View PHP project resources on IBM developerworks to expand PHP skills.
For interesting interviews and discussions with software developers, visit the developerworks podcast.
Should I use the database with PHP? View Zend core for IBM, which is a seamless, ready-to-use, easy-to-install PHP development and production environment that supports IBM DB2 V9.
Stay tuned to technical events and network broadcasts on developerworks.
Check out recent seminars, trade exhibitions, network broadcasts, and other activities for IBM open source code developers to be held globally.
Visit the open source code area on developerworks for a wide range of how-to information, tools, and project updates to help you develop with open source technology and use it with IBM products.
View the free developerworks on demand demo and learn about IBM and open-source technologies and product features.
Obtain products and technologies
Download PHP v5.2.3.
Download symfony.
Download CakePHP.
Use IBM trial software to improve your next development project, which can be obtained by downloading or from a DVD.
Download IBM product evaluation and start using application development tools and middleware Products for DB2, Lotus, rational, Tivoli and websphere.
Discussion
Join the developerworks blog and join the developerworks community.
Join developerworks PHP Forum: Developing PHP applications with IBM information management products (DB2, IDS ).
About the author
Duane O 'Brien has been technically competent since Oregon Trail was just a text. His favorite food is sushi. He has never been to the moon.