Created
May 20, 2013 10:40
-
-
Save samsonasik/5611544 to your computer and use it in GitHub Desktop.
module specific setting in ZF1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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