A presentation at PHPWM User Group in in Birmingham, UK by Rob Allen
Introduction to the Zend Framework By Rob Allen West Midlands PHP User Group 8th February 2007
What this presentation will cover ■ ■ ■ ■ Overview of the Zend Framework Introduction to the ZF MVC System Hello World example The other ZF components
Who am I? ■ ■ ■ ■ ■ Software engineer since 1995 Started with PHP3 in 1999 Contributor to the Zend Framework Written intro tutorial on www.akrabat.com Writing a book on the Zend Framework
Why Use the Zend Framework? ■ ■ ■ ■ (Will be) A full-stack framework Modern design: OO PHP 5 Easy to learn Rapid development
Why Use the Zend Framework? ■ ■ ■ ■ Documented Vibrant community BSD license Free from IP disagreements
What’s in the Zend Framework? Core: Zend_Controller Zend_View Zend_Db Zend_Config Zend_Filter Zend_Session Zend_Acl Zend_Auth Http Zend_Http_Client/Server Zend_Uri Internationalization: Zend_Date Zend_Locale Web Services Zend_Feed Zend_Gdata Zend_Service_Amazon/Flickr/ Yahoo Inter-application communication Zend_Json Zend_XmlRpc Zend_Soap Zend_Rest Advanced Zend_Cache Zend_Search Zend_Pdf Zend_Mail/Zend_Mime
M-V-C ■ ■ ■ ■ Separation of control, display and business logic Model: The “business logic” View: The pretty bit! Controller: The “glue”
M: Zend_Db_Table ■ ■ ■ Table gateway Not an ActiveRecord! Uses Zend_Db abstraction layer
V: Zend_View ■ ■ ■ ■ PHP based like Savant Form Helpers Security via escape() Trivial to implement Smarty/PHPTAL
C: Zend_Controller_Front ■ ■ ■ ■ Front Controller pattern Request & Response provided URLs: /controller/action/p1/v1/p2/v2 Flex points RewriteRouter Module support Dispatcher
Show me some code! ■ ■ Highlights from the Tutorial Simple Music Album application List albums Add/edit/delete albums
Directory Structure this_site/ /application /controllers /models /views /library /Zend /public /images /scripts /styles
Bootstrap: index.php // load configuration $config = new Zend_Config_Ini(‘./application/config.ini’, ‘general’); Zend::register(‘config’, $config); // setup database $db = Zend_Db::factory($config->db->adapter, $config->db->config->asArray()); Zend_Db_Table::setDefaultAdapter($db); // register the view we are going to use $view = new Zend_View(); $view->setScriptPath(‘./application/views’); Zend::register(‘view’, $view);
Aside: Config.ini [general] debug = false db.adapter = PDO_MYSQL db.config.host = localhost db.config.username = rob db.config.password = 123456 db.config.dbname = zftest [dev : general] debug = true ■ ■ Inherited Sections Nested namespaces: $config->db->adapter
Bootstrap: index.php // setup controller $front = Zend_Controller_Front::getInstance(); $front->setControllerDirectory( ‘./application/controllers’); $front->throwExceptions(true); // run! $front->dispatch();
Controller: IndexController.php Class IndexController extends Zend_Controller_Action { function indexAction() { $view = Zend::registry(‘view’); $view->title = “My Albums”; $album = new Album(); $view->albums = $album->fetchAll(); } } $view->actionTemplate = ‘index/index.tpl.php’; $this->_response->setBody( $view->render(‘site.tpl.php’));
View: site.tpl.php <html> <head> <title><?php echo $this->escape($this->title);?></title> <link rel=”stylesheet” type=”text/css” media=”screen” href=”/zf-tutorial/public/styles/site.css” /> </head> <body> <div id=”content”> <?php echo $this->render($this->actionTemplate); ?> </div> </body> </html>
View: index/index.tpl.php <h1><?php echo $this->escape($this->title); ?></h1> <p><a href=”/zf-tutorial/index/add”>Add new album</a></p> <table> <tr><th>Title</th><th>Artist</th><th> </th></tr> <?php foreach($this->albums as $album) : ?> <tr> <td><?php echo $this->escape($album->title);?></td> <td><?php echo $this->escape($album->artist);?></td> <td><?php echo $this->EditandDelLinks(album->id);?> </td> </tr> <?php endforeach; ?> </table>
View Helper: Money class Zend_View_Helper_money { public function money($value) { $output = ”; $value = trim($value); if(is_numeric($value)) { if($value >= 0) { $output = ‘£’ . number_format($value, 2); } else { $output = ‘-£’ . number_format(substr($value, 1), 2); } } return $output; } }
Model: Album.php <?php class Album extends Zend_Db_Table { } That’s All!
The results
The other components ■ ■ ■ ■ ■ ■ ■ Authentication Search PDF Email Caching Localisation Web Services Google, Amazon, Yahoo! Flickr, etc