PHPBelgium Arteveldehogeschool Campus Mariakerke meeting 20-08-2008 Extending Zend Framework
Why Zend Framework ? <ul><li>most adopted framework by </li></ul><ul><ul><li>enterprises </li></ul></ul><ul><ul><li>(new m...
Components in ZF
Why extending components ? <ul><li>no rewrite or re-invention of the wheel </li></ul><ul><li>re-use functionality that's a...
Setting up your library <ul><li>Having Zend Framework in </li></ul><ul><li>/approot/library/Zend </li></ul><ul><li>Your ow...
Example <ul><li>Extending the Zend_Translator adapter </li></ul><ul><ul><li>to use a database backend </li></ul></ul><ul><...
Optimizing own library <ul><li>When extending Zend Framework </li></ul><ul><ul><li>use same directory structure as ZF </li...
Understand the component <ul><li>Zend_Translate </li></ul><ul><ul><li>loads all translations for a locale </li></ul></ul><...
How do we start ? <?php /** * @see Zend_Translate_Adapter */ require_once 'Zend/Translate/Adapter.php'; /** * MyComp_Trans...
load from database /** * Loads the translation data into the Adapter * @param string|array $data – DB related settings *  ...
our custom logic // we create a select statement first $select = $options['dbAdapter']->select(); $select->from($data['tab...
putting it all back // we put everything back into data so it can be handled properly  // by the translation adapter. $dat...
finishing touch /** * returns the adapters name * * @return string */ public function toString() { return &quot;Db&quot;; }
using the new adapter // Setting up the translation parameters $data = array( 'dbAdapter'  => $dbAdapter, // already set i...
convenient view helper class MyComp_View_Helper_Tr { protected $_view; public function setView(Zend_View_Interface $view) ...
helper magic public function Tr($label) { $default = &quot;en_US&quot;; $string = null; if (Zend_Register::isRegistered('t...
Summary <ul><li>Extending ZF compontents </li></ul><ul><ul><li>modify the bare minimum </li></ul></ul><ul><ul><li>follow g...
Thank you... Questions ?
Support our cause <ul><li>PHPBelgium needs your help </li></ul><ul><ul><li>suggest topics for next meetings </li></ul></ul...
Upcoming SlideShare
Loading in...5
×

Extending Zend Framework

6,360

Published on

Zend Framework covers many day-to-day solutions and fits perfectly for most jobs, but if you\'re facing a challenge that\'s not yet covered in the framework, you can easily extend components to fit your needs. This way you still have the power of Zend Framework without having to re-write all from scratch.

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,360
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
119
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Extending Zend Framework

  1. 1. PHPBelgium Arteveldehogeschool Campus Mariakerke meeting 20-08-2008 Extending Zend Framework
  2. 2. Why Zend Framework ? <ul><li>most adopted framework by </li></ul><ul><ul><li>enterprises </li></ul></ul><ul><ul><li>(new media) start-ups </li></ul></ul><ul><ul><li>governments </li></ul></ul><ul><li>it's growing community </li></ul><ul><li>simple approach for complicated tasks </li></ul>
  3. 3. Components in ZF
  4. 4. Why extending components ? <ul><li>no rewrite or re-invention of the wheel </li></ul><ul><li>re-use functionality that's already made </li></ul><ul><li>modify only the action you need </li></ul><ul><li>rely on the framework </li></ul><ul><li>better code management </li></ul><ul><ul><li>when upgrading ZF </li></ul></ul><ul><ul><li>when upgrading own code-base </li></ul></ul>
  5. 5. Setting up your library <ul><li>Having Zend Framework in </li></ul><ul><li>/approot/library/Zend </li></ul><ul><li>Your own library should be in </li></ul><ul><li>/approot/library/Mycomp </li></ul>
  6. 6. Example <ul><li>Extending the Zend_Translator adapter </li></ul><ul><ul><li>to use a database backend </li></ul></ul><ul><ul><li>extending Zend_Translate_Adapter </li></ul></ul>
  7. 7. Optimizing own library <ul><li>When extending Zend Framework </li></ul><ul><ul><li>use same directory structure as ZF </li></ul></ul>approot/ Mycomp/ Translate/ Adapter/ Db.php Zend/ ..
  8. 8. Understand the component <ul><li>Zend_Translate </li></ul><ul><ul><li>loads all translations for a locale </li></ul></ul><ul><ul><li>multiple locales can be added </li></ul></ul><ul><ul><li>can be used with caching </li></ul></ul><ul><ul><li>interacts with other components (e.g. Zend_Form) </li></ul></ul><ul><ul><li>extends on Zend_Translate_Adapter </li></ul></ul>
  9. 9. How do we start ? <?php /** * @see Zend_Translate_Adapter */ require_once 'Zend/Translate/Adapter.php'; /** * MyComp_Translate_Adapter_Db * Translation adapter for using a database back-end * */ class Mycomp_Translate_Adapter_Db extends Zend_Translate_Adapter { }
  10. 10. load from database /** * Loads the translation data into the Adapter * @param string|array $data – DB related settings * - dbAdapter: adapter to connect with DB * - tableName: storage of the translation * - options: filter on the translations * @param string $locale – The locale this translation is for * @param array $options – general adapter options */ protected function _loadTranslationData($data, $locale, $options) { ... }
  11. 11. our custom logic // we create a select statement first $select = $options['dbAdapter']->select(); $select->from($data['tableName'], array($data['keyField'], $data['valueField'])) ->where('language = ?', $locale); $stmt = $select->query(); // we store our whole translation into $trData $trData = $stmt->fetchAll(); // we create a scheme to generate a Zend_Translate_Array type $trScheme = array (); if (!empty ($trData)) { foreach ($trData as $trEl) { $trScheme[$trEl[$data['keyField']]] = $trEl[$data['valueField']]; } }
  12. 12. putting it all back // we put everything back into data so it can be handled properly // by the translation adapter. $data = $trScheme; // we need to check if we don't call a clean up routine $options = array_merge($this->_options, $options); if (($options['clear'] == true) || !isset($this->_translate[$locale])) { $this->_translate[$locale] = array(); } // now we merge our data back to the translate adapter $this->_translate[$locale] = array_merge($this->_translate[$locale], $data); /** * NOTE: $this->_options and $this->_translate are properties * from the parent class Zend_Translate_Adapter */
  13. 13. finishing touch /** * returns the adapters name * * @return string */ public function toString() { return &quot;Db&quot;; }
  14. 14. using the new adapter // Setting up the translation parameters $data = array( 'dbAdapter' => $dbAdapter, // already set in the bootstrap 'tableName' => 'translation', 'localeField' => 'language', 'keyField' => 'key', 'valueField' => 'value', ); $options = array (); try { $translate = new Zend_Translate('MyComp_Translate_Adapter_Db', $data, 'en_US', $options); $translate->addTranslation($data, 'nl_BE', $options); $translate->addTranslation($data, 'fr_BE', $options); } catch (Zend_Translate_Exception $zte) { echo “<h1>” . $zte->getMessage() . “</h1>” . PHP_EOL; }
  15. 15. convenient view helper class MyComp_View_Helper_Tr { protected $_view; public function setView(Zend_View_Interface $view) { $this->_view = $view; } }
  16. 16. helper magic public function Tr($label) { $default = &quot;en_US&quot;; $string = null; if (Zend_Register::isRegistered('translator')) { $translator = Zend_Register::get('translator'); if ($translator->isTranslated($label)) { $string = $translator->_($label); } else { $translator->setLocale(new Zend_Locale($default)); $string = $translator->_($label); } } else { $string = $label; } return $string; }
  17. 17. Summary <ul><li>Extending ZF compontents </li></ul><ul><ul><li>modify the bare minimum </li></ul></ul><ul><ul><li>follow general guidelines </li></ul></ul><ul><ul><li>comment your code as much as possible </li></ul></ul><ul><ul><li>put your extension in YOUR library </li></ul></ul><ul><li>Can also be applied to general classes </li></ul>
  18. 18. Thank you... Questions ?
  19. 19. Support our cause <ul><li>PHPBelgium needs your help </li></ul><ul><ul><li>suggest topics for next meetings </li></ul></ul><ul><ul><li>give a presentation at our meetings </li></ul></ul><ul><ul><li>interact with us on </li></ul></ul><ul><ul><ul><li>irc (freenode) #php_bnl </li></ul></ul></ul><ul><ul><ul><li>twitter.com/PHPBelgium </li></ul></ul></ul><ul><ul><li>small donations are welcome :-p </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×