Posted By

nikefido on 12/03/10


Tagged

zend boostrap


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

piotrlewandowski
ef


Sample Zend boot strap file - layout, views, registry, error handling


 / Published in: PHP
 

Sample Zend bootstrap and supporting files 1) Sets custom layout depending on module used 2) Adds view helpers - footscript for adding scripts to footer and custom bodyClass helper 3) Sets baseUrl option for view and layout 4) Sets error handler to a specific module 5) Sets custom .ini options into registry

  1. <?php
  2.  
  3. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  4. {
  5.  
  6. protected function _initLayoutHelper()
  7. {
  8. $this->bootstrap('frontController');
  9. $layout = Zend_Controller_Action_HelperBroker::addHelper(
  10. new AppName_Controller_Action_Helper_LayoutLoader());
  11.  
  12. $doctypeHelper = new Zend_View_Helper_Doctype();
  13. $doctypeHelper->doctype('HTML5');
  14. }
  15.  
  16. protected function _initViewHelpers() {
  17. $this->bootstrap('layout');
  18. $layout = $this->getResource('layout');
  19. $view = $layout->getView();
  20.  
  21. $bodyClass = new AppName_Helper_BodyClass();
  22. $view->registerHelper($bodyClass, 'BodyClass');
  23.  
  24. $footScript = new AppName_Helper_FootScript();
  25. $view->registerHelper($footScript, 'FootScript');
  26.  
  27. $baseUrl = $this->getOption('baseUrl');
  28. $view->baseUrl = $baseUrl;
  29. $layout->baseUrl = $baseUrl; }
  30.  
  31. protected function _initErrorHandler() {
  32. $front = Zend_Controller_Front::getInstance();
  33. $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
  34. 'module' => 'error',
  35. 'controller' => 'error',
  36. 'action' => 'error'
  37. )));
  38. }
  39.  
  40. protected function _initRegistry() {
  41. Zend_Registry::set('facebook', $this->getOption('facebook'));
  42.  
  43. $googleApi = $this->getOption('google');
  44. Zend_Registry::set('googleApi', $googleApi['api']);
  45. }
  46. }
  47.  
  48.  
  49. //related files:
  50. class AppName_Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
  51. {
  52.  
  53. public function preDispatch()
  54. {
  55. $module = $this->getRequest()->getModuleName();
  56. if ($module == 'app') {
  57. $this->getActionController()
  58. ->getHelper('layout')
  59. ->setLayout('app');
  60. }
  61. }
  62.  
  63. }
  64.  
  65. class AppName_Helper_FootScript extends Zend_View_Helper_HeadScript {
  66.  
  67. protected $_regKey = 'Zend_View_Helper_FootScript'; //Need different namespace than standard _HeadScript view helper
  68.  
  69. public function footScript() {
  70. return parent::headScript();
  71. }
  72. }
  73.  
  74. class AppName_Helper_BodyClass extends Zend_View_Helper_Placeholder_Container_Standalone {
  75.  
  76. private $_classes = array();
  77.  
  78. public function __construct($classes = null) {
  79. if(is_array($classes)) {
  80. $this->addClass($classes);
  81. }
  82. }
  83.  
  84. public function addClass($class) {
  85. if(is_array($class)) {
  86. foreach($class as $c) {
  87. if(is_string($c)) {
  88. $this->addClass($c); //recursion
  89. } else {
  90. throw new Zend_Exception('Class must be a string - is type: '.gettype($c));
  91. }
  92. }
  93. return $this;
  94. }
  95.  
  96. if(is_string($class)) {
  97. $this->_classes[] = $class;
  98. return $this;
  99. } else {
  100. throw new Zend_Exception('Class must be a string - is type: '.gettype($class));
  101. }
  102. return $this;
  103. }
  104.  
  105. public function removeClass($class) {
  106. $key = array_search($class, $this->_classes);
  107. if($key !== false) {
  108. unset($this->_classes[$key]);
  109. }
  110. return $this;
  111. }
  112.  
  113. public function bodyClass() {
  114. return $this;
  115. }
  116.  
  117. public function toString() {
  118. return implode(' ', $this->_classes);
  119. }
  120. }
  121. ?>
  122.  
  123. <!-- HERE IS A SAMPLE LAYOUT FILE USING ABOVE VIEW HELPERS -->
  124. <?php echo $this->doctype() ?>
  125. <html>
  126.  
  127. <head>
  128. <meta charset="utf-8">
  129.  
  130. <!-- www.phpied.com/conditional-comments-block-downloads/ -->
  131. <!--[if IE]><![endif]-->
  132.  
  133. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  134.  
  135. <?php echo $this->headTitle() ?>
  136. <meta name="description" content="">
  137. <meta name="author" content="">
  138. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
  139. <?php echo $this->headMeta() ?>
  140.  
  141. <link rel="shortcut icon" href="<?php echo $this->layout()->baseUrl; ?>/favicon.png">
  142. <link rel="apple-touch-icon" href="<?php echo $this->layout()->baseUrl; ?>/apple-touch-icon.png">
  143.  
  144. <link rel="stylesheet" href="<?php echo $this->layout()->baseUrl; ?>/css/app/imports.css">
  145. <?php echo $this->headLink() ?>
  146. <?php echo $this->headStyle() ?>
  147.  
  148. <?php echo $this->headScript() ?>
  149. <!--[if IE]>
  150. <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  151. <![endif]-->
  152. <base href="/" />
  153. </head>
  154.  
  155. <body class="<?php echo $this->bodyClass(); ?>">
  156. <div id="fb-root"></div>
  157. <?php echo $this->layout()->content ?>
  158.  
  159. <!-- Scripts -->
  160. <script src="<?php echo $this->layout()->baseUrl; ?>/js/cssbrowsers.js"></script>
  161.  
  162. <!-- Grab Google CDN's jQuery. fall back to local if necessary -->
  163. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  164. <!-- <script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"></script>')</script> -->
  165. <?php echo $this->footScript() ?>
  166. <script src="<?php echo $this->layout()->baseUrl; ?>/js/plugins.js"></script>
  167. <script src="<?php echo $this->layout()->baseUrl; ?>/js/script.js"></script>
  168.  
  169.  
  170. <script>
  171. var _gaq = [['_setAccount', 'UA-XXXXX-X'], ['_trackPageview']];
  172. (function(d, t) {
  173. var g = d.createElement(t),
  174. s = d.getElementsByTagName(t)[0];
  175. g.async = true;
  176. g.src = '//www.google-analytics.com/ga.js';
  177. s.parentNode.insertBefore(g, s);
  178. })(document, 'script');
  179. </script>
  180. </body>
  181. </html>

Report this snippet  

You need to login to post a comment.