• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Extending Zend Framework
 

Extending Zend Framework

on

  • 9,856 views

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 ...

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.

Statistics

Views

Total Views
9,856
Views on SlideShare
9,822
Embed Views
34

Actions

Likes
6
Downloads
117
Comments
0

2 Embeds 34

http://www.slideshare.net 33
http://translate.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Extending Zend Framework Extending Zend Framework Presentation Transcript

    • PHPBelgium Arteveldehogeschool Campus Mariakerke meeting 20-08-2008 Extending Zend Framework
    • Why Zend Framework ?
      • most adopted framework by
        • enterprises
        • (new media) start-ups
        • governments
      • it's growing community
      • simple approach for complicated tasks
    • Components in ZF
    • Why extending components ?
      • no rewrite or re-invention of the wheel
      • re-use functionality that's already made
      • modify only the action you need
      • rely on the framework
      • better code management
        • when upgrading ZF
        • when upgrading own code-base
    • Setting up your library
      • Having Zend Framework in
      • /approot/library/Zend
      • Your own library should be in
      • /approot/library/Mycomp
    • Example
      • Extending the Zend_Translator adapter
        • to use a database backend
        • extending Zend_Translate_Adapter
    • Optimizing own library
      • When extending Zend Framework
        • use same directory structure as ZF
      approot/ Mycomp/ Translate/ Adapter/ Db.php Zend/ ..
    • Understand the component
      • Zend_Translate
        • loads all translations for a locale
        • multiple locales can be added
        • can be used with caching
        • interacts with other components (e.g. Zend_Form)
        • extends on Zend_Translate_Adapter
    • 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 { }
    • 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) { ... }
    • 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']]; } }
    • 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 */
    • 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 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; }
    • convenient view helper class MyComp_View_Helper_Tr { protected $_view; public function setView(Zend_View_Interface $view) { $this->_view = $view; } }
    • 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; }
    • Summary
      • Extending ZF compontents
        • modify the bare minimum
        • follow general guidelines
        • comment your code as much as possible
        • put your extension in YOUR library
      • Can also be applied to general classes
    • Thank you... Questions ?
    • Support our cause
      • PHPBelgium needs your help
        • suggest topics for next meetings
        • give a presentation at our meetings
        • interact with us on
          • irc (freenode) #php_bnl
          • twitter.com/PHPBelgium
        • small donations are welcome :-p