Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 108 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
108
Dung lượng
1,8 MB
Nội dung
614 CHAPTER 24 ■ MVC AND THE ZEND FRAMEWORK // Accessed through http://www.example.com/ public function indexAction() { $this->view->title = "Welcome to Our Chess Club Web Site!"; } } ?> This script creates a view property named title that will be used to assign the Web page’s title. Finally, create one more controller, AboutController.php, intended to display information pertinent to the Web site’s purpose and, for sake of demonstration, some information about the visiting user. This controller is displayed in Listing 24-3. Listing 24-3. The AboutController Controller (AboutController.php) <?php // Load the Zend_Controller_Action class require_once('Zend/Controller/Action.php'); class AboutController extends Zend_Controller_Action { // Accessed through http://www.example.com/about/ public function indexAction() { $this->view->title = "About Our Chess Club"; } // Accessed through http://www.example.com/about/you/ public function youAction() { // Page title $this->view->title = "About You!"; Gilmore_862-8C24.fm Page 614 Monday, February 25, 2008 9:56 AM CHAPTER 24 ■ MVC AND THE ZEND FRAMEWORK 615 // Retrieve the user's IP address $this->view->ip = $_SERVER['REMOTE_ADDR']; // Retrieve browser information $this->view->browser = $_SERVER['HTTP_USER_AGENT']; } } ?> Creating the Views Next, create the views that correspond to these three actions: one for the home page, one for the /about/ page, and one for the /about/you/ page. Place the homepage view in the directory /application/modules/default/views/scripts/index/, and the other two in /application/modules/default/views/scripts/about/. These views are presented in Listings 24-4, 24-5, and 24-6, respectively. Each of these views is intended to demon- strate different facets of the behavior of views. Listing 24-4. The index.phtml View <?php echo $this->render('header.phtml'); ?> <div id="header">Next Chess Club Meeting: April 12</div> <p> Welcome to our Chess Club's Web site! We're a bunch of chess enthusiasts who travel the globe in search of worthy opponents. Join us at our next meeting, held at the coffee shop on the corner of Third and Neil each Tuesday at 6 p.m. </p> <?php echo $this->render('footer.phtml'); ?> Gilmore_862-8C24.fm Page 615 Monday, February 25, 2008 9:56 AM 616 CHAPTER 24 ■ MVC AND THE ZEND FRAMEWORK Listing 24-5. The about.phtml View <?php echo $this->render('header.phtml'); ?> <div id="header">About Our Chess Club</div> <p> Founded: 1997<br /> City: Columbus, Ohio<br /> Where we meet: Cup of Love, corner of Third and Neil<br /> When we meet: Each Tuesday at 6 p.m.<br /> Notes: Bring your board and pieces if you have them! </p> <?php echo $this->render('footer.phtml'); ?> Listing 24-6. The you.phtml View <?php echo $this->render('header.phtml'); ?> <div id="header">About You!</div> <p> Your IP Address: <?php echo $this->escape($this->ip); ?><br /> Your Browser: <?php echo $this->escape($this->browser); ?><br /> </p> <?php echo $this->render('footer.phtml'); ?> As demonstrated in these views, you should pass all data originating in the controller through the escape() method, as it will properly filter data through PHP’s htmlspecialchars() function. Gilmore_862-8C24.fm Page 616 Monday, February 25, 2008 9:56 AM CHAPTER 24 ■ MVC AND THE ZEND FRAMEWORK 617 You’ll see each of these views refer to header.phtml and footer.phtml files (both of which are available at the book’s Source Code/Download page at http:// www.apress.com), which serve as the page template headers and footers, respectively. These global templates can be placed in the /application/modules/default/views/ scripts/ directory and will automatically be located and integrated into the view when using the render() method. Not surprisingly, the header could include refer- ences to the page masthead as well as the CSS and JavaScript files. The footer could include things such as copyright information and the closing page tags. ■Tip Quite conveniently, the Zend Framework supports the ability to take advantage of more sophis- ticated templating solutions than those demonstrated here, such as Smarty (see Chapter 19). See the Zend Framework manual for more information. Trying It Out With the actions and views defined, it’s time for the moment of truth. Try navigating to the following pages and see what happens: • To access the home page, navigate to this URL: http://localhost/. • To access the about.phtml view, navigate to this URL: http://localhost/about/. • To access the you.phtml view, navigate to this URL: http://localhost/about/you/. Next, consider experimenting by adding a new action and class and set of corre- sponding views. Just copy and rename one of the controllers, being sure to follow the same conventions used in the original class. Searching the Web with Zend_Service_Yahoo Table 24-1 presented just some of the dozens of Zend Framework components at your disposal; therefore, as you might imagine, it’s difficult to decide which compo- nent to demonstrate in this brief chapter. After some consideration, it seems ideal to introduce the Zend_Service_Yahoo component, as it shows how the framework can really shine at simplifying otherwise fairly complex operations, in this case Web Services interaction. Gilmore_862-8C24.fm Page 617 Monday, February 25, 2008 9:56 AM 618 CHAPTER 24 ■ MVC AND THE ZEND FRAMEWORK The Zend_Service_Yahoo component allows you to plug into Yahoo!’s search engine, as well as search images, businesses, and news. Therefore, suppose you want to add a page to the chess club Web site that displays the latest chess news. This news page will appear at http://www.example.com/news/, meaning you need to add a new controller and view. ■Note In order to follow along with these examples, you need to register for a free Yahoo! application ID. Navigate to http://developer.yahoo.com/ for more information. Creating the Controller The controller, named NewsController.php, should be placed in the application/ modules/default/controllers directory. This controller is responsible for retrieving the news via the Yahoo! component and sending that data to the view. The NewsController.php script is found in Listing 24-7. Listing 24-7. The Chess Club’s News Controller (NewsController.php) <?php // Load the Zend_Controller_Action class require_once('Zend/Controller/Action.php'); // Load the Yahoo! Service class require_once('Zend/Service/Yahoo.php'); class NewsController extends Zend_Controller_Action { public function indexAction() { // Invoke the Yahoo! Service $yahoo = new Zend_Service_Yahoo("INSERT_YAHOO_ID_HERE"); // Execute the search $results = $yahoo->newsSearch("chess"); Gilmore_862-8C24.fm Page 618 Monday, February 25, 2008 9:56 AM CHAPTER 24 ■ MVC AND THE ZEND FRAMEWORK 619 // Send the search results to the view $view->results = $results; } } Of course, in a real-world situation you might use the controller to retrieve some user preferences from a database pertinent to region, allowing for more geographi- cally targeted chess-related news results. Those preferences could then be passed to the view much in the same way the other properties were passed in previous examples. Creating the View The view’s role is simple: render the search results in an easily readable format. This is done by looping through each result and outputting it to the browser. This file, named index.phtml, should be placed in the directory application/modules/default/views/ scripts/news/. Listing 24-8 presents this simple but effective view. Listing 24-8. The Chess Club’s News View (index.phtml) <?php echo $this->render('header.phtml'); ?> <h4>The Latest Chess News</h4> <?php foreach ($this->results as $result) { printf("<p><a href='%s'>%s</a> | %s <br />", $this->escape($result->NewsSourceUrl), $this->escape($result->NewsSource), date("F j, Y", $this->escape($result->PublishDate)) ); printf("%s </p>", $this->escape($result->Summary)); } ?> <?php echo $this->render('footer.phtml'); ?> Gilmore_862-8C24.fm Page 619 Monday, February 25, 2008 9:56 AM 620 CHAPTER 24 ■ MVC AND THE ZEND FRAMEWORK Executing this code produces news-related output similar to that shown in Figure 24-1. Figure 24-1. Output of the latest chess news Summary This chapter provided but a glimpse of what the Zend Framework is capable of, but hopefully it has served its purpose: to get your mind racing about just how productive Web frameworks can make you. The next chapter presents a formal introduction to MySQL. Gilmore_862-8C24.fm Page 620 Monday, February 25, 2008 9:56 AM 621 ■ ■ ■ CHAPTER 25 Introducing MySQL Believe it or not, the MySQL relational database server was born out of an internal company project spearheaded by employees of the Sweden-based TcX DataKonsult AB (AB is an abbreviation for Aktiebolag, which is the Swedish term for corporation). Their project, dubbed MySQL, was first released to the general public at the end of 1996. The software proved so popular that in 2001 they founded a company based entirely around MySQL-specific service and product offerings, calling it MySQL AB. Profitable since its inception, MySQL AB has since grown by leaps and bounds, estab- lishing offices in several countries, attracting substantial venture capital funding, and announcing numerous high-profile partnerships with an array of corporate heavy- weights, including Red Hat, Veritas, Novell, and Rackspace. From its first public release in 1996, MySQL’s developers placed particular emphasis on software performance and scalability. The result was a highly optimized product that was lacking in many features considered standard for enterprise database prod- ucts: stored procedures, triggers, and transactions, for example. Yet the product caught the attention of a vast number of users who were more interested in speed and scalability than in capabilities that would, in many cases, often go unused anyway. Subsequent versions included additional features, which attracted even more users. MySQL has been downloaded more than 100 million times in its lifetime, with more than 50,000 daily downloads (http://www.mysql.com/company/factsheet.html). These users include some of the most widely known companies and organizations in the world, such as Yahoo!, CNET Networks, NASA, The Weather Channel, Google, the Chicago Mercantile Exchange, and Cisco Systems (http://www.mysql.com/customers/). Later, this chapter takes a closer look at how a few of these users are putting MySQL to work and, in some cases, saving millions of dollars in the process. Gilmore_862-8C25.fm Page 621 Friday, February 15, 2008 7:12 AM 622 CHAPTER 25 ■ INTRODUCING MYSQL What Makes MySQL So Popular? MySQL is a relational database server that inches closer to the features you’ll find in competing proprietary products with each release, meaning you won’t encounter too many surprises if you’re familiar with another database product. Its well-known convenient pricing aside (free for many uses), what else is it about MySQL that makes it so popular? This section highlights some of the key features contributing to its soaring popularity. Afterward, some specific information is offered pertinent to two major milestone releases of the MySQL product, namely versions 4 and 5. Flexibility No matter what operating system you’re running, chances are MySQL has you covered. On the MySQL Web site, you’ll find optimized binaries available for 14 platforms, including Compaq Tru64, DEC OSF, FreeBSD, IBM AIX, HP-UX, Linux, Mac OS X, Novell NetWare, OpenBSD, QNX, SCO, SGI IRIX, Solaris (versions 8 and 9), and Microsoft Windows. Packages are also available for Red Hat, SUSE, and Ubuntu. Furthermore, MySQL makes the source code available for download if binaries are not available for your platform, or if you want to perform the compilation yourself. A wide array of APIs is also available for all of the most popular programming languages, including C, C++, Java, Perl, PHP, Ruby, and Tcl, among others. MySQL also offers many types of mechanisms for managing data; these are known as storage engines. The importance of taking care to choose a particular storage engine is analogous to the importance of using an appropriate algorithm for a particular task. Like algorithms, storage engines are particularly adept at certain tasks and may be maladapted for others. MySQL has long supported several engines, namely MyISAM (the default on all operating systems except Windows), MEMORY (previously known as HEAP), InnoDB (the default on Windows), and MERGE. Version 5 added the ARCHIVE, BLACKHOLE, CSV, FEDERATED, and EXAMPLE engines. More recently, MySQL has released an alpha version of Falcon, a high-performance storage engine intended for large-scale deployments on SMP systems. Gilmore_862-8C25.fm Page 622 Friday, February 15, 2008 7:12 AM [...]... /usr/src /mysql- VERSION-OS.tar.gz 3 Link the installation directory to a common denominator: %>ln -s FULL-PATH -TO -MYSQL- VERSION-OS mysql 4 Install the MySQL database mysql_ install_db is a shell script that logs in to the MySQL database server, creates all of the necessary tables, and populates them with initial values %>cd mysql %>chown -R mysql %>chgrp -R mysql %>scripts /mysql_ install_db user =mysql. .. In total, the following topics are covered: • Downloading instructions • Distribution variations • Installation procedures (source, binary, RPMs) • Setting the MySQL administrator password • Starting and stopping MySQL • Installing MySQL as a system service • MySQL configuration and optimization issues • Reconfiguring PHP to use MySQL By the chapter’s conclusion, you’ll have learned how to install and. .. application requires MySQL to operate and will be released under the GPL or a GPL-compatible license, you’re free to use the MySQL Open Source License and therefore use MySQL free of charge • If your application requires customers to install a version of MySQL to operate it but you are not going to release it under the GPL or a GPL-compatible license, then you need to purchase a MySQL Commercial License... following command offers a succinct description of the package architecture and its contents: %>rpm -qp info MySQL- server-standard-VERSION.rpm Executing the following command displays all packaged files and their installation destination: %>rpm -qpl MySQL- server-standard-VERSION.rpm Installing the MySQL RPMs You can install the MySQL RPM with a single command: %>rpm -i MySQL- server-standard-VERSION.rpm... this book, you need to download at least two files, replacing VERSION with the version information of your particular RPM choice: • The MySQL server (MySQL- server-standard-VERSION.rpm) • The MySQL client (MySQL- client-standard-VERSION.rpm) Download these packages, saving them to your preferred distribution repository directory It’s typical to store packages in the /usr/src directory, but the location... %>chown -R mysql data That’s it! Proceed to the section “Setting the MySQL Administrator Password.” Installing and Configuring MySQL on Windows Open source products continue to make headway on the Microsoft Windows server platform, with historically predominant Unix-based technologies like the Apache Web server, PHP, the Perl and Python programming languages, and, more recently, MySQL continuing to gain... installation allows you to wield total control over what’s installed, in addition to allowing you to choose the installation directory Go ahead and choose the Custom installation and click Next 5 Either accept or change the installation directory The default is C:\ Program Files \MySQL\ MySQL Server 5.0\ For easy access you might consider changing this to C: \mysql Click Next, and then click Install in the... (Essentials and Complete) Note that prior to name changes between MySQL 4.1.2 and 4.1.3, this binary was named mysqld-opt mysqld-debug Includes support for debugging and for InnoDB and BDB tables Only available with the Complete version mysqld-max Optimized binary with support for InnoDB and for symbolic links Only available with the Complete version Versions prior to MySQL 5.1.12 also support BDB tables mysqld-max-nt... Chapter 27; Chapter 29 delves into issues pertinent to MySQL users and the MySQL privilege system The process for stopping the MySQL server on Linux and Windows follows: shell>cd INSTALL-DIR/bin shell>mysqladmin -u root -p shutdown Enter password: ******* Assuming that you supply the proper credentials, you will be returned to the command prompt without notification of the successful shutdown of the MySQL. .. classified ads, and disseminates more than 2 million new job listings 631 Gilmore_862-8C25.fm Page 632 Friday, February 15, 2008 7: 12 AM 632 CHAPTER 25 ■ INTRODUC ING MYSQL According to a case study published at MySQL. com, titled “craigslist Relies on MySQL to Serve Millions of Classified Ads” (http://www .mysql. com/why -mysql/ case-studies /mysql- craigslist-casestudy.pdf), craigslist depends upon MySQL to run . hail from English-speaking countries, and thus MySQL enables users to choose from more than 35 character sets. You can use these char- acter sets to control the language used for error and status. devoted to MySQL configuration, and the whole of Chapter 29 is dedicated to MySQL security. Gilmore_862-8C25.fm Page 625 Friday, February 15, 2008 7: 12 AM 626 CHAPTER 25 ■ INTRODUCING MYSQL Flexible. application requires MySQL to operate and will be released under the GPL or a GPL-compatible license, you’re free to use the MySQL Open Source License and therefore use MySQL free of charge. •