Note: This excerpt does not include the lesson files. The lesson files are available with purchase of the book.
What You Will Learn
In this lesson, you will:
- Install the Zend Framework and set up site-specific code hints
- Alter a database table to add a unique index and extra columns
- Validate user input on the server with Zend_Validate
- Preserve user input and display error messages when input fails validation
- Create reusable code with Dreamweaver’s Server Behavior Builder
- Check for duplicate usernames
- Insert user input into a database with Zend_Db
- Create a login system with Zend_Auth
This lesson takes approximately 3 hours to complete.
In this lesson, with the help of a powerful third-party script library—Zend Framework— you’ll create a robust user registration system that validates input on the server. This involves writing your own PHP code rather than relying on Dreamweaver to generate it for you. Any inconvenience is more than made up for in greater functionality, and the process is simplified by site-specific code hints.
You’ll also use Dreamweaver’s Server Behavior Builder to speed up the insertion of frequently used PHP code.
Figure 1 The registration form alerts the user to errors before inserting details into the database.
Introducing the Zend Framework
The Zend Framework (ZF) is a huge open source library of PHP scripts. The “minimal” version of ZF 1.10 consists of more than 2,700 files in nearly 500 folders, and is more than 22 MB in size. Of course, size isn’t everything. In fact, you might wonder why you need so many files to do a few basic tasks, such as inserting and updating records in a database, uploading files, and sending emails.
Frameworks provide a wide range of options, many of which you might never use. For example, ZF has 14 components that access different e-commerce services on Amazon.com. Most developers never use them, but they’re indispensible if you have an Amazon affiliate account. Instead of writing a script of mind-numbing complexity, you can query the Amazon database with just a few lines of code.
Most tutorials assume you want to use ZF to build a web application using the Model-View-Controller (MVC) design pattern, which divides data handling (the model), output (the view), and conditional logic (the controller) among separate scripts. Unless you have considerable PHP experience, the MVC design pattern can be confusing, so this book takes a different approach, offering a gentler introduction to ZF.
ZF is a loosely coupled framework, which means each part is designed to work with minimal dependency on other parts. You can use just a few components without needing to learn the whole framework. But should you decide later to adopt MVC, your knowledge of key ZF components will speed up the transition.
Here are the main components you’ll be using in the remaining lessons:
- Zend_Auth to check user credentials
- Zend_Db to communicate with a database
- Zend_File to upload files
- Zend_Loader to load ZF classes automatically
- Zend_Mail to send emails and attachments
- Zend_Paginator to page through a long series of database results
- Zend_Service_ReCaptcha to prevent spam input
- Zend_Validate to validate user input
The exercises continue using MySQL, but Zend_Db makes the code more flexible. Changing just one or two lines allows you to switch to Microsoft SQL Server, PostgreSQL, or another database system.
ZF has a number of other factors in its favor:
- Its principal sponsor is Zend Technologies, the company founded and run by PHP core contributors.
- Leading software companies, including Adobe, Google, and Microsoft, have contributed components or significant features. Adobe contributed Zend_Amf, which acts as a gateway between PHP and the Flash Player, using the binary Action Message Format (AMF).
- ZF was designed from the outset to use PHP 5 objects. Many frameworks were originally designed for compatibility with PHP 4, which is less efficient.
- The next major version of ZF is being designed to enhance interoperability with other frameworks, such as Symfony.
Installing the Zend Framework
The CD accompanying this book contains ZendFramework-1.10.6-minimal.zip. Alternatively, download the most recent version from http://framework.zend.com/download/latest. Choose the Minimal version of ZF, which contains all the files you need. Some download links require you to establish a Zend account. Like ZF, this is free and does not entail any obligations.
To install ZF, simply unzip the file to a suitable location on your hard disk. It’s more efficient to locate it outside the phpcs5 site root, so it’s accessible to all sites in Dreamweaver. Create a folder called php_library at the top level of your C drive on Windows or in your home folder on a Mac, and unzip ZF there.
Two folders, bin and library, are inside the main folder. The framework is in a subfolder of library called Zend. Each component has a corresponding .php file in the Zend folder plus a folder of its own.
There’s no need to open the files, but if you do, you’ll see that each file is extensively commented, which partly explains why the framework is so big. As you gain more experience, you’ll discover a lot of useful information in these comments. Sometimes they help explain features that are not fully documented.
Setting up site-specific code hints for ZF
In Lesson 4, you learned how to create site-specific code hints for WordPress, Drupal, and Joomla! Dreamweaver automatically recognizes the structure of these CMSs. With other frameworks, including ZF, you need to tell Dreamweaver where to find the files and which ones you want to scan to generate code hints. You don’t need the WordPress code hints again in this book, so the following instructions show how to set up a separate structure for ZF hints. This replaces the existing version of dw_php_codehinting.config in the phpcs5 site, but you can easily switch between different sets of code hints by selecting them in the Structure menu of the Site-Specific Code Hints dialog box.
- In the Dreamweaver Files panel, select the PHP CS5 site.
- Make sure the active file in the Document window is from the current site, or close all files.
- Choose Site > Site-Specific Code Hints.
- Make sure the Structure menu is set to <New from Blank>.
- Click the “Select sub-root folder” icon next to the Sub-root text box, and navigate to the library folder one level above the Zend folder.
- Click Select. Because the folder is outside the site root, Dreamweaver displays the following alert.
- Click the plus (+) button above the File(s) area to open the Add File/Folder dialog box.
- Click the “Select folder” icon next to the File/Folder text box, and select the Zend folder.
- The Recursive checkbox tells Dreamweaver whether to scan all subfolders. If you select the checkbox for the Zend folder, code hints are enabled for the entire framework. Because ZF contains so many files and folders, scanning all of them is not a good idea, so leave the checkbox deselected.
- To make the scanning process more efficient, click the plus button above the Extensions area, and type .php in the highlighted section. This tells Dreamweaver to scan only .php files.
- Click Add, and repeat steps 7–10 to add the following folders, all of which are subfolders of Zend: Auth, Captcha, Db, File, Loader, Mail, Paginator, and Validate. When setting up each folder, select the Recursive checkbox and add .php to the Extensions list.
- Save the configuration by clicking the Save Structure icon at the top right of the dialog box .
- Type Zend in the Name text box (you can use any name except Custom, Drupal, Joomla, or WordPress), and click Save.
- Click OK to close the Site-Specific Code Hints dialog box. This inserts a file called dw_php_codehinting.config in the site root. Dreamweaver uses this to scan the ZF folders and create code hints for the selected components.
Just click OK.
You also need to add the Service folder and one of its subfolders, ReCaptcha. The Service folder contains many subfolders, so select the Recursive checkbox only for the ReCaptcha subfolder.
If you forget to select the Recursive checkbox or add the .php filename extension, you can change the options using the checkbox and button at the bottom of the Site-Specific Code Hints dialog box.