First Steps With Zend Framework

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

2 comments

Comments 1 - 2 of 2 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

7 Favorites & 1 Event

First Steps With Zend Framework - Presentation Transcript

  1. First steps with Zend Framework Rob Allen
  2. Rob Allen? PHPNW ’08 Rob Allen http://akrabat.com
  3. What is ZF? • Use-at-will PHP5 Framework • Open source - BSD license • Documented • Quality assured • Certification • Actively maintained by Zend PHPNW ’08 Rob Allen http://akrabat.com
  4. ZF Philosophy • Use-at-will • Simple usage 80% of the time • Agile practices • Showcase current trends PHPNW ’08 Rob Allen http://akrabat.com
  5. ZF Components PHPNW ’08 Rob Allen http://akrabat.com
  6. Getting started • Use individual components in your current applications • Start a new application using the ZF MVC components PHPNW ’08 Rob Allen http://akrabat.com
  7. Using Zend_Cache in an existing application
  8. The current code function getNews(PDO $dbh) { $stmt = $dbh->query('SELECT * FROM news ORDER BY date_created DESC'); $stmt->setFetchMode(PDO::FETCH_ASSOC); $list = $stmt->fetchAll(); return $list; } $news = getNews($dbh); PHPNW ’08 Rob Allen http://akrabat.com
  9. The HTML <h1>News</h1> <ul> <?php foreach($news as $row) : ?> <li><?= $row['title'] ?> - <?= $row['date_created'] ?></li> <?php endforeach; ?> </ul> PHPNW ’08 Rob Allen http://akrabat.com
  10. Zend_Cache diagram PHPNW ’08 Rob Allen http://akrabat.com
  11. Add Zend_Cache 1. Add Zend Framework to lib/Zend folder 2. Create a cache data folder 3. Set up the cache 4. Wrap cache code around database query 5. Thatʼs it! PHPNW ’08 Rob Allen http://akrabat.com
  12. Zend_Cache set-up function initCache($cacheDir) { require_once('Zend/Cache.php'); $feOpts = array( 'lifetime' => '7200', 'automatic_serialization'=>true); $bkOpts = array( 'cache_dir' => $cacheDir); $cache = Zend_Cache::factory('Core', 'File', $feOpts, $bkOpts); return $cache; } PHPNW ’08 Rob Allen http://akrabat.com
  13. Zend_Cache in use function getNewsFromCache(PDO $dbh, Zend_Cache_Core $cache) Unique { $cacheId = 'latestNews'; $list = $cache->load($cacheId); if($list === false) { Re-use $list = getNews($dbh); existing $cache->save($list, $cacheId); code } return $list; } PHPNW ’08 Rob Allen http://akrabat.com
  14. Zend_Cache in use $cacheDir = dirname(__FILE__).'/cache'; $cache = initCache($cacheDir); $news = getNewsFromCache($dbh, $cache); (the HTML does not change) PHPNW ’08 Rob Allen http://akrabat.com
  15. Useful components for integrating into an existing code base: • Cache • PDF • Search • Auth: Captcha, OpenId, LDAP, InfoCard • Web services, Logging and WildFire • Mail PHPNW ’08 Rob Allen http://akrabat.com
  16. A new Zend Framework application
  17. Model View Controller (Most Vexing Conundrum) PHPNW ’08 Rob Allen http://akrabat.com
  18. ZFʼs MVC PHPNW ’08 Rob Allen http://akrabat.com
  19. Routing: URLs /controller/action/param1/value1/ or create your own: /news/2008/11 $route = new Zend_Controller_Router_Route( 'news/:year/:month', array( 'controller' => 'news', 'action' => 'archive' ), ); $router->addRoute('newsarchive', $route); PHPNW ’08 Rob Allen http://akrabat.com
  20. Directory structure Our code ZF Overrides application data config library controllers App helpers Zend IndexController.php public forms css Apache serves layouts img these files helpers js scripts index.php layout.phtml .htaccess models tests views controllers helpers models scripts AllTests.php index index.phtml PHPNW ’08 Rob Allen http://akrabat.com
  21. Bootstrapping public/.htaccess public/index.php application/bootstrap.php PHPNW ’08 Rob Allen http://akrabat.com
  22. .htaccess php_value date.timezone \"UTC\" php_value short_open_tag \"1\" For view files php_value error_reporting \"8191\" php_value display_errors \"1\" Don’t do this in production! RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] Redirect to index.php RewriteRule ^.*$ index.php [NC,L] if request doesn’t exist PHPNW ’08 Rob Allen http://akrabat.com
  23. index.php define('BASE_PATH', realpath(dirname(__FILE__) . '/')); define('APPLICATION_ENVIRONMENT', 'development'); set_include_path(BASE_PATH . '/library' . PATH_SEPARATOR . get_include_path()); require_once \"Zend/Loader.php\"; Zend_Loader::registerAutoload(); require BASE_PATH .'/application/bootstrap.php'; bootstrap(); Process Zend_Controller_Front::getInstance()->dispatch(); request PHPNW ’08 Rob Allen http://akrabat.com
  24. bootstrap.php function bootstrap() { $config = new Zend_Config_Ini( BASE_PATH . '/application/config/app.ini', APPLICATION_ENVIRONMENT); Zend_Registry::set('config', $config); Zend_Registry::set('env', APPLICATION_ENVIRONMENT); Singleton // Front Controller $frontController = Zend_Controller_Front::getInstance(); $frontController->setControllerDirectory( BASE_PATH . 'application/controllers'); } PHPNW ’08 Rob Allen http://akrabat.com
  25. IndexController.php class IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->headTitle('Home'); $this->view->title = 'Welcome'; } } PHPNW ’08 Rob Allen http://akrabat.com
  26. Composite View PHPNW ’08 Rob Allen http://akrabat.com
  27. Set up View Initialise in bootstrap(): Zend_Layout::startMvc(BASE_PATH . 'application/layouts/scripts'); $view = Zend_Layout::getMvcInstance()->getView(); $view->doctype('XHTML1_STRICT'); $view->headTitle()->setSeparator(' - '); PHPNW ’08 Rob Allen http://akrabat.com
  28. layout.phtml <?= $this->doctype() ?> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <?= $this->headTitle('My Website');?> <?= $this->headLink()->prependStylesheet( $this->baseUrl().'/css/global.css') ?> </head> <body> <div id=\"header\"></div> <div id=\"content\"><?= $this->layout()->content ?></div> <div id=\"footer\"></div> </body></html> PHPNW ’08 Rob Allen http://akrabat.com
  29. index.phtml <h1><?= $this->title; ?></h1> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam lobortis nibh ac quam. Aenean urna nisi, semper ut, porttitor quis, tempor sed, eros. Etiam porta lorem. etc.</p> <p><a href=\"<?= $this->url(array('controller'=>'about', 'action'=>'contact')); ?>\">Contact us</a></p> PHPNW ’08 Rob Allen http://akrabat.com
  30. View helper <img src=\"<?= $this->baseUrl() ?>/img/logo.jpg\" /> class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract { function baseUrl() { $fc = Zend_Controller_Front::getInstance(); return $fc->getBaseUrl(); } } Return, don’t echo! PHPNW ’08 Rob Allen http://akrabat.com
  31. Model The business logic: • Database • Web services • Local files PHPNW ’08 Rob Allen http://akrabat.com
  32. Database Zend_Db_Adapter - Database abstraction Zend_Db_Table - Table Data Gateway PHPNW ’08 Rob Allen http://akrabat.com
  33. Image of Zend_Db PHPNW ’08 Rob Allen http://akrabat.com
  34. Set up database config/app.ini: database.adapter = PDO_MYSQL database.params.username = rob database.params.password = itsasecret! database.params.dbname = phpnw in bootstrap(): $db = Zend_Db::factory($config->database); Zend_Db_Table_Abstract::setDefaultAdapter($db); Zend_Registry::set('dbAdapter', $db); PHPNW ’08 Rob Allen http://akrabat.com
  35. A Model PHPNW ’08 Rob Allen http://akrabat.com
  36. Table class class Authors extends Zend_Db_Table_Abstract { protected $_name = 'authors'; protected $_rowClass = 'Author'; public function fetchBySurname($surname) { $select = $this->select(); $select->where('surname LIKE ?', $surname.'%'); $select->order('surname ASC'); $rowset = $this->fetchAll($select); return $rowset; } } PHPNW ’08 Rob Allen http://akrabat.com
  37. Row class class Author extends Zend_Db_Table_Row_Abstract { public function name() { return $this->forename . ' ' . $this->surname; } protected function _insert() { $this->date_created = date('Y-m-d H:i:s'); } } PHPNW ’08 Rob Allen http://akrabat.com
  38. Relationships PHPNW ’08 Rob Allen http://akrabat.com
  39. One to many PHPNW ’08 Rob Allen http://akrabat.com
  40. One to many class Books extends Zend_Db_Table_Abstract { protected $_name = 'books'; protected $_rowClass = 'Book'; protected $_referenceMap = array( 'Author' => array( 'columns' => 'author_id', 'refTableClass' => 'Authors', 'refColumns' => 'id' ) ); PHPNW ’08 Rob Allen http://akrabat.com
  41. Fetching 1:N class Author extends Zend_Db_Table_Row_Abstract { public function fetchBooks() { return $this->findDependentRowset('Books'); } } // Controller $authorsTable = new Authors(); $douglasAdams = $authorsTable->fetchById(2); $books = $douglasAdams->fetchBooks(); PHPNW ’08 Rob Allen http://akrabat.com
  42. The other way class Book extends Zend_Db_Table_Row_Abstract { public function fetchAuthor() { return $this->findParentRow('Authors'); } } // Controller $booksTable = new Books(); $hhgttg = $booksTable->fetchById(1); $douglasAdams = $hhgttg->fetchAuthor(); PHPNW ’08 Rob Allen http://akrabat.com
  43. Many to many PHPNW ’08 Rob Allen http://akrabat.com
  44. Many to many class BooksCategories extends Zend_Db_Table_Abstract { protected $_name = 'books_categories'; protected $_referenceMap = array( 'Book' => array( 'columns' => array('book_id'), 'refTableClass' => 'Books', 'refColumns' => array('id') ), 'Category' => array( 'columns' => array('category_id'), 'refTableClass' => 'Categories', 'refColumns' => array('id') ) ); } PHPNW ’08 Rob Allen http://akrabat.com
  45. Fetching M:N class Book extends Zend_Db_Table_Row_Abstract { public function fetchCategories() { return $this->findManyToManyRowset( 'Categories', 'BooksCategories'); } } // Controller $booksTable = new Books(); $zfia = $booksTable->fetchById(2); $categories = $zfia->fetchCategories(); PHPNW ’08 Rob Allen http://akrabat.com
  46. Thatʼs it! (The basics, anyway)
  47. Getting involved • Mailing list • Maintain the wiki • Submit bug reports • Fix bugs! (sign the CLA first...) • Propose new components PHPNW ’08 Rob Allen http://akrabat.com
  48. Available resources • http://framework.zend.com • Community • Books - Buy my book! http://www.zendframeworkinaction.com • Consultancy PHPNW ’08 Rob Allen http://akrabat.com
  49. Thank you Provide feedback on this talk: http://joind.in/81 PHPNW ’08 Rob Allen http://akrabat.com

+ Rob AllenRob Allen, 11 months ago

custom

5035 views, 7 favs, 6 embeds more stats

An introduction to Zend Framework given at PHPNW 08 more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 5035
    • 4152 on SlideShare
    • 883 from embeds
  • Comments 2
  • Favorites 7
  • Downloads 191
Most viewed embeds
  • 866 views on http://akrabat.com
  • 12 views on http://static.slideshare.net
  • 2 views on http://nohuyasdelaquimica.blogspot.com
  • 1 views on http://203.208.37.104
  • 1 views on http://www.agglom.com

more

All embeds
  • 866 views on http://akrabat.com
  • 12 views on http://static.slideshare.net
  • 2 views on http://nohuyasdelaquimica.blogspot.com
  • 1 views on http://203.208.37.104
  • 1 views on http://www.agglom.com
  • 1 views on http://192.168.10.100

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories

Groups / Events