我试图在 CustomControllerAction.php 中获取 $this->moduleName 的值,但我在模块 javascript 和一般模块之间得到了不同的值。
对于: http: //myip.com/username/general 回显结果为“一般”
但是对于: http: //myip.com/username/javascript 回显结果是“默认”,但我知道它应该是“javascript”。
为什么模块 javascript 无法获取其模块名称?
我使用 Zend Framework 模块化结构和 Smarty 进行视图展示。
谢谢你。
这是我的代码:
/modules/javascript/controlles/IndexController.php
<?php
class Javascript_IndexController extends modules_CustomControllerAction
{
public function init()
{
/* Initialize action controller here */
parent::init();
$view = new EZ_View_SmartyJS($this->config->smarty->toArray());
$view->setScriptPath(APPLICATION_PATH.'/modules/javascript/views/theme_classic/js/');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
$viewRenderer->setViewSuffix($this->config->smarty->javascriptSuffix);
$this->_helper->layout->disableLayout();
$this->getResponse()
->setHeader('Content-Type', 'text/javascript');
$this->params = $this->getRequest()->getParams();
$this->view->params = $this->params;
}
}
?>
/modules/general/controlles/IndexController.php
<?php
class General_IndexController extends modules_CustomControllerAction{
public function init()
{
parent::init();
// initialize smarty view
$view = new EZ_View_Smarty($this->config->smarty->toArray());
$view->setScriptPath(APPLICATION_PATH.'/modules/general/views/theme_classic/scripts/');
// setup viewRenderer with suffix and view
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setViewSuffix('tpl');
$viewRenderer->setView($view);
$layout = Zend_Layout::getMvcInstance();
$layout->setViewSuffix('tpl');
/**
* Set inflector for Zend_Layout
*/
$inflector = new Zend_Filter_Inflector(':script.:suffix');
$inflector->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower'),
'suffix' => 'tpl'));
// Initialise Zend_Layout's MVC helpers
$theme = 'theme_classic';
Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH . '/modules/themes/'.$theme.'/layout/',
'view' => $view,
'contentKey' => 'general',
'inflector' => $inflector));
}
}
?>
/application/Bootstrap.php
<?php
protected function _initRoutes()
{
$this->bootstrap('frontcontroller');
$front = $this->getResource('frontcontroller');
$router = $front->getRouter();
$router->addRoute('root', new Zend_Controller_Router_Route(
'/',
array(
'module' => 'home',
'controller' => 'index',
'action' => 'index'
)
));
$router->addRoute('javascript', new Zend_Controller_Router_Route(
':username/javascript/:action/*',
array(
'controller' => 'index'
)
));
$router->addRoute('default', new Zend_Controller_Router_Route(
':username/:module/:action/*',
array(
'controller' => 'index'
)
));
$router->addRoute('index', new Zend_Controller_Router_Route(
':username/:module',
array(
'controller' => 'index',
'action' => 'index'
)
));
return $router;
}
?>
/application/modules/CustomControllerAction.php
<?php
public function init(){
$controller = Zend_Controller_Front::getInstance();
$this->moduleName = $controller->getRequest()->getModuleName();
$this->controllerName = $controller->getRequest()->getControllerName();
$this->actionName = $controller->getRequest()->getActionName();
$this->userName = $controller->getRequest()->getParam('username');
echo moduleName; //<------------------test result
}
?>