Introduction to Zend Framework

A presentation at PHPWM User Group in February 2007 in Birmingham, UK by Rob Allen

Slide 1

Slide 1

Introduction to the Zend Framework By Rob Allen West Midlands PHP User Group 8th February 2007

Slide 2

Slide 2

What this presentation will cover ■ ■ ■ ■ Overview of the Zend Framework Introduction to the ZF MVC System Hello World example The other ZF components

Slide 3

Slide 3

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

Slide 4

Slide 4

Why Use the Zend Framework? ■ ■ ■ ■ (Will be) A full-stack framework Modern design: OO PHP 5 Easy to learn Rapid development

Slide 5

Slide 5

Why Use the Zend Framework? ■ ■ ■ ■ Documented Vibrant community BSD license Free from IP disagreements

Slide 6

Slide 6

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

Slide 7

Slide 7

M-V-C ■ ■ ■ ■ Separation of control, display and business logic Model: The “business logic” View: The pretty bit! Controller: The “glue”

Slide 8

Slide 8

M: Zend_Db_Table ■ ■ ■ Table gateway Not an ActiveRecord! Uses Zend_Db abstraction layer

Slide 9

Slide 9

V: Zend_View ■ ■ ■ ■ PHP based like Savant Form Helpers Security via escape() Trivial to implement Smarty/PHPTAL

Slide 10

Slide 10

C: Zend_Controller_Front ■ ■ ■ ■ Front Controller pattern Request & Response provided URLs: /controller/action/p1/v1/p2/v2 Flex points RewriteRouter Module support Dispatcher

Slide 11

Slide 11

Show me some code! ■ ■ Highlights from the Tutorial Simple Music Album application List albums Add/edit/delete albums

Slide 12

Slide 12

Directory Structure this_site/ /application /controllers /models /views /library /Zend /public /images /scripts /styles

Slide 13

Slide 13

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);

Slide 14

Slide 14

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

Slide 15

Slide 15

Bootstrap: index.php // setup controller $front = Zend_Controller_Front::getInstance(); $front->setControllerDirectory( ‘./application/controllers’); $front->throwExceptions(true); // run! $front->dispatch();

Slide 16

Slide 16

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’));

Slide 17

Slide 17

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>

Slide 18

Slide 18

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>

Slide 19

Slide 19

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; } }

Slide 20

Slide 20

Model: Album.php <?php class Album extends Zend_Db_Table { } That’s All!

Slide 21

Slide 21

The results

Slide 22

Slide 22

The other components ■ ■ ■ ■ ■ ■ ■ Authentication Search PDF Email Caching Localisation Web Services Google, Amazon, Yahoo! Flickr, etc