Okay,
This is completely a weird situation.
When a user logs in, I store some stuff using the following code:
$auth->getStorage()->write($authAdapter->getResultRowObject(array(
'username',
'avatar',
'status',
'role',
'id',
'email'
)));
Then I use the following code in my bootstrap to get access to the variables:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function preDispatch()
{
$this->frontController = Zend_Controller_Front::getInstance();
}
protected function _initBuildBase()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$this->view = $layout->getView();
// Set doctype
$this->view->doctype("XHTML1_TRANSITIONAL");
$this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
}
protected function _initLoader()
{
$adminLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Admin',
'basePath' => APPLICATION_PATH));
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('MyApp_');
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'model' => array(
'path' => 'models/',
'namespace' => 'Models_'
),
'service' => array(
'path' => 'services/',
'namespace' => 'Services_'
),
'form' => array(
'path' => 'forms/',
'namespace' => 'Forms_'
),
),
));
return $autoLoader;
return $adminLoader;
}
protected function _initControllerPlugins()
{
$this->frontController->registerPlugin(new MyApp_Controller_Plugin_ApplicationSettings);
$this->frontController->registerPlugin(new MyApp_Controller_Plugin_LayoutLoader);
$this->frontController->registerPlugin(new MyApp_Controller_Plugin_LanguageSelector);
$this->frontController->registerPlugin(new MyApp_Controller_Plugin_Acl);
$this->frontController->registerPlugin(new MyApp_Controller_Plugin_Uploadify);
}
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/views/helpers');
}
pro开发者_开发知识库tected function _initRoutes()
{
$router = $this->frontController->getRouter();
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:module/:controller/:action/:id', array('module' => 'admin', 'controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:module/:controller/index/:page', array('module' => 'admin', 'controller' => ':controller', 'action' => 'index', 'page' => ':page'))
);
$router->addRoute(
'pageUrl',
new Zend_Controller_Router_Route('/:page/:subpage', array('module' => 'default', 'controller' => 'index', 'action' => 'index', 'page' => ':page', 'subpage' => ':subpage'))
);
}
protected function _initLogin()
{
$this->auth = Zend_Auth::getInstance();
$this->view->user = $this->auth->getIdentity();
}
}
Okay, and now for the weird part:
When I use
if($this->user->role == 'Administrator')
{
In my layout.phtml it works great. But when I use the same code in a index.phtml file which is loaded according to the controller it doesn't work?!
The stranger part is that it used to work perfectly and use it t show different options depending of the user's role.
Where could I look for mistakes in my code? Really lost on places to check on. Probably changed something somewhere, but everything else seems to work fine.
Any tips or directions would be awesome!
The layout and view resource both do use the Zend_Controller_Action_Helper_ViewRenderer()
. The layout resource accesses the helper to get the view (readonly). But the view resource accesses the helper and overwrites the view object of the helper.
So in your case the layout got bootstrapted, you did set the variables. Afterwards the view resource is bootstrapted which overwrites the view in the helper.
You always should bootstrap the view before the layout to ensure they both use the same view object.
精彩评论