Skip to content

Instantly share code, notes, and snippets.

@samsonasik
Created May 20, 2013 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonasik/5611544 to your computer and use it in GitHub Desktop.
Save samsonasik/5611544 to your computer and use it in GitHub Desktop.
module specific setting in ZF1
1. create Controller Plugin that has routeShutdown(Zend_Controller_Request_Abstract $request)
class Zfgen_Controller_Boot extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$frontController = Zend_Controller_Front::getInstance();
$bootstrap = $frontController->getParam('bootstrap');
$activeModuleName = $request->getModuleName();
$moduleList = $bootstrap->modules;
$moduleInitName = strtolower($activeModuleName) . "Init";
$moduleInitNameLength = strlen($moduleInitName);
if (isset($moduleList[$activeModuleName])) {
$activeModule = $moduleList[$activeModuleName];
$bootstrapMethodNames = get_class_methods($bootstrap);
foreach ($bootstrapMethodNames as $key => $method) {
$runMethod = false;
$methodNameLength = strlen($method);
if ($moduleInitNameLength < $methodNameLength &&
$moduleInitName == substr($method, 0, $moduleInitNameLength)) {
call_user_func(array($bootstrap, $method));
}
}
} else {
$activeModule = $bootstrap;
}
$methodNames = get_class_methods($activeModule);
foreach ($methodNames as $key => $method) {
$runMethod = false;
$methodNameLength = strlen($method);
if (10 < $methodNameLength && 'activeInit' === substr($method, 0, 10)) {
$runMethod = true;
} elseif ($moduleInitNameLength < $methodNameLength &&
$moduleInitName == substr($method, 0, $moduleInitNameLength)) {
$runMethod = true;
}
if ($runMethod) {
call_user_func(array($activeModule, $method));
}
}
}
}
2. register the plugin at application/Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules/default'
)
);
return $autoloader;
}
public function _initModule()
{
$this->frontController = Zend_Controller_Front::getInstance();
$this->router = $this->frontController->getRouter();
$this->frontController->registerPlugin(new Zfgen_Controller_Boot());
}
}
3. create a function under modules/modulename/Bootstrap.php with detected from routeShutdown method of controller plugin.
class Mobile_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function activeInitMobile()
{
$theme = Zend_Registry::get('theme');
Zend_Layout::startMvc(array(
'layoutPath' => PUBLIC_PATH . "/themes/$theme/layoutmobile/scripts",
'layout' => 'layout'
));
}
}
YOU CAN DEFINE BY NAMING FUNCTION : activeInitMODULENAME.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment