Return to Snippet

Revision: 37045
at December 3, 2010 02:57 by nikefido


Initial Code
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

	protected function _initLayoutHelper()
	{
		$this->bootstrap('frontController');
		$layout = Zend_Controller_Action_HelperBroker::addHelper(
		    new AppName_Controller_Action_Helper_LayoutLoader());
		
	    $doctypeHelper = new Zend_View_Helper_Doctype();
		$doctypeHelper->doctype('HTML5');
	}
	
	protected function _initViewHelpers() {
		$this->bootstrap('layout');
		$layout = $this->getResource('layout');
		$view = $layout->getView();
		
		$bodyClass = new AppName_Helper_BodyClass();
		$view->registerHelper($bodyClass, 'BodyClass');
		
		$footScript = new AppName_Helper_FootScript();
		$view->registerHelper($footScript, 'FootScript');
		
		$baseUrl = $this->getOption('baseUrl');
		$view->baseUrl = $baseUrl;
		$layout->baseUrl = $baseUrl;	}
	
	protected function _initErrorHandler() {
		$front = Zend_Controller_Front::getInstance();
		$front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
		    'module'     => 'error',
		    'controller' => 'error',
		    'action'     => 'error'
		)));
	}
	
	protected function _initRegistry() {
		Zend_Registry::set('facebook', $this->getOption('facebook'));
		
		$googleApi = $this->getOption('google');
		Zend_Registry::set('googleApi', $googleApi['api']);
	}
}


//related files:
class AppName_Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
{

    public function preDispatch()
    {
        $module = $this->getRequest()->getModuleName();
        if ($module ==  'app') {
            $this->getActionController()
                 ->getHelper('layout')
                 ->setLayout('app');
        }
    }
    
}

class AppName_Helper_FootScript extends Zend_View_Helper_HeadScript {
	
	protected $_regKey = 'Zend_View_Helper_FootScript'; //Need different namespace than standard _HeadScript view helper
	
	public function footScript() {
		return parent::headScript();
	}
}

class AppName_Helper_BodyClass extends Zend_View_Helper_Placeholder_Container_Standalone {
	
	private $_classes = array();
	
	public function __construct($classes = null) {
		if(is_array($classes)) {
			$this->addClass($classes);
		}
	}
	
	public function addClass($class) {
		if(is_array($class)) {
			foreach($class as $c) {
				if(is_string($c)) {
					$this->addClass($c); //recursion
				} else {
					throw new Zend_Exception('Class must be a string - is type: '.gettype($c));
				}
			}
			return $this;
		}
		
		if(is_string($class)) {
			$this->_classes[] = $class;
			return $this;
		} else {
			throw new Zend_Exception('Class must be a string - is type: '.gettype($class));
		}
		return $this;
	}
	
	public function removeClass($class) {
		$key = array_search($class, $this->_classes);
		if($key !== false) {
			unset($this->_classes[$key]);
		}
		return $this;
	}
	
	public function bodyClass() {
		return $this;
	}
	
	public function toString() {
		return implode(' ', $this->_classes);
	}
}
?>

<!-- HERE IS A SAMPLE LAYOUT FILE USING ABOVE VIEW HELPERS -->
<?php echo $this->doctype() ?>
<html>

<head>
	<meta charset="utf-8">
	
		<!-- www.phpied.com/conditional-comments-block-downloads/ -->
		<!--[if IE]><![endif]-->
	
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	
		<?php echo $this->headTitle() ?>
		<meta name="description" content="">
		<meta name="author" content="">
		<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
		<?php echo $this->headMeta() ?>
	
		<link rel="shortcut icon" href="<?php echo $this->layout()->baseUrl; ?>/favicon.png">
		<link rel="apple-touch-icon" href="<?php echo $this->layout()->baseUrl; ?>/apple-touch-icon.png">
	
		<link rel="stylesheet" href="<?php echo $this->layout()->baseUrl; ?>/css/app/imports.css">
		<?php echo $this->headLink() ?>
		<?php echo $this->headStyle() ?>
		
		<?php echo $this->headScript() ?>
		<!--[if IE]>
		<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
		<![endif]-->   
    <base href="/" />
</head>

<body class="<?php echo $this->bodyClass(); ?>">
	<div id="fb-root"></div>
    <?php echo $this->layout()->content ?>
    
    <!-- Scripts -->
	<script src="<?php echo $this->layout()->baseUrl; ?>/js/cssbrowsers.js"></script>
	
	<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
	<!-- <script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"></script>')</script> -->
	<?php echo $this->footScript() ?>
	<script src="<?php echo $this->layout()->baseUrl; ?>/js/plugins.js"></script>
	<script src="<?php echo $this->layout()->baseUrl; ?>/js/script.js"></script>
	

	<script>
	 var _gaq = [['_setAccount', 'UA-XXXXX-X'], ['_trackPageview']];
	 (function(d, t) {
		var g = d.createElement(t),
				s = d.getElementsByTagName(t)[0];
		g.async = true;
		g.src = '//www.google-analytics.com/ga.js';
		s.parentNode.insertBefore(g, s);
	 })(document, 'script');
	</script>
</body>
</html>

Initial URL


Initial Description
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

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

Initial Tags

 

Initial Language
PHP