I am new with zend and I have a problem changing my layout in the bootstrap. I want to change my layout when the user is logged in.
My function to change the layout in the bootstrap is like this:
protected function _initAuthState()
{
$layout = new Zend_Layout;
$layout->setLayoutPath('/layouts/scripts');
if (Zend_Auth::getInstance()->hasIdentity()):
// Logged in.
$layout->setLayout(layout2);
else:
// Not Logged in.
$layout->setLayout(‘layout’);
endif;
}
This code doesn't work, the layout is always the same ... help!
You are modifying a new layout instance, not the instance that is being used by the system.
I assume you are specifying your layout params in application.ini. So you need:
$this->bootstrap('layout');
$layout = $this->getResource('layout');
Then perform your check/modification on this layout instance.
BTW, changing layout is often done using a front-controller plugin. Still runs early enough to do the job, but is often more configurable and re-usable. See here and here for two examples.
I found the answer!!
this is my final result, and is working!!
Bootstrap.php:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initLoader(){
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => '../application/',
'namespace' => 'My',
));
$resourceLoader->addResourceTypes(array(
'plugin' => array(
'path' => 'plugins/',
'namespace' => 'Plugin',
)
));
}
public function _initPlugins()
{
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Plugin_Layout());
}
}
application/plugins/Layout.php:
<?php
class My_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function preDispatch()
{
$user = Zend_Auth::getInstance();
$role = $user->getIdentity()->role;
$layout = Zend_Layout::getMvcInstance();
switch ($role) {
case 'admin':
$layout->setLayout('layout2');
break;
case 'normal':
$layout->setLayout('layout');
break;
default:
$layout->setLayout('layout');
break;
}
}
}
?>
Related
I want to pass some value from controller to view partial, basically, view partial is set using the placeholder in Application\Module.php, here is the code that does it.
<?php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
use Zend\ServiceManager\ServiceManager;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'initBreadcrumbAndActionButtonWidget'));
$moduleRouteListener->attach($eventManager);
$serviceManager = $e->getApplication()->getServiceManager();
}
public function initBreadcrumbAndActionButtonWidget(\Zend\Mvc\MvcEvent $e)
{
$serviceManager = $e->getApplication()->getServiceManager();
$config = $serviceManager->get('config');
$viewHelperManager = $this->getViewHelperManager($e->getApplication()->getServiceManager());
$partialHelper = $viewHelperManager->get('partial');
$partial = $partialHelper('widgets/breadcrumb', array(
'config' => $config,
'actionButtons' => $config['action-buttons']
));
$placeHolder = $viewHelperManager->get('placeholder');
$placeHolder('widgets/breadcrumb')->set($partial);
}
public function getViewHelperManager($serviceManager)
{
$stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view/')));
$resolver = new Resolver\AggregateResolver();
$resolver->attach($stack);
$renderer = new PhpRenderer();
$renderer->setResolver($resolver);
$viewHelperManager = $serviceManager->get('ViewHelperManager');
return $viewHelperManager;
}
public function getConfig()
{
return array_merge_recursive(
include __DIR__ . '/config/module.config.php',
);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
It is all working good till here, however now i want to pass a value from controller to the partial widgets/breadcrumb, after several attempt i thought implementing a ViewHelper could be a solution, and i implemented following view helper.
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Variable extends AbstractHelper
{
protected $data;
public function __invoke()
{
return $this;
}
public function set($identifier, $data)
{
$this->data[$identifier] = $data;
return $this;
}
public function get($identifier)
{
return $this->data[$identifier];
}
}
In controller i assign the value to ViewHelper like this.
$viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
$variable = $viewHelperManager->get('variable');
$variable->set('stack', 'overflow');
And in partial, to access the value i use the get method.
$this->variable()->get('stack');
Now my problem is that get() is rendered first hence the value set in controller has no effect.
My question is how can i make the set() gets rendered first so i can have the value in partial, or if there is any better approach of passing the value to partial please suggest.
Thanks.
Baaah, i called the view helper in wrong action, it worked when i moved the $variable->set('stack', 'overflow'); in the action where i wanted the value to pass.
Not sure if there can be a better solution, but this does the job for me to pass the value after i found no way in ZF2 that satisfies my requirement.
When i go to controllers index URL preDispatch is called before entering indexAction method of that controller but postDispatch is not called after it. Why it is not called and should i force it somehow to call postDispatch?
I have two plugin with postDispach methods whitch are not called.
If i got to URL e.g admin/listperms postDispatch is called and everything works.
AdminController.php
class AdminController extends Application_Controllers_Base {
public function indexAction() {
$this->view->headTitle = $this->alias('TITLE_ADMIN_PANEL');
}
public function listpermsAction() {
$this->view->headTitle = $this->alias('TITLE_PERMISSION_LIST');
$permService = new Application_Services_AdminPermission();
$perms = $permService->getPermissionList();
if(!$perms) {
// TODO:
} else {
$this->view->permList = $perms;
}
}
}
Base.php
class Application_Controllers_Base extends Zend_Controller_Action {
}
DbLogPlugin.php
class Application_Plugin_DbLogPlugin extends Zend_Controller_Plugin_Abstract {
public function postDispatch(Zend_Controller_Request_Abstract $request) {
$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
$view->developerLog = $this->getQuerysLogs();
}
}
bootstrap.php
protected function _initAutoload(){
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application_', //'
'basePath' => APPLICATION_PATH
));
$autoloader->addResourceType('dao', 'dao', 'Dao');
$autoloader->addResourceType('services', 'services', 'Services');
$autoloader->addResourceType('plugins', 'plugins', 'Plugins');
$autoloader->addResourceType('models', 'models', 'Models');
$autoloader->addResourceType('controllers', 'controllers', 'Controllers');
require_once('controllers/FrontController.php');
$front = Application_Controllers_FrontController::getInstance();
$front->setControllerDirectory(APPLICATION_PATH.'/controllers')
->setRouter(new Zend_Controller_Router_Rewrite())
->registerPlugin(new Application_Plugin_DbLogPlugin())
->registerPlugin(new Application_Plugin_AuthPlugin())
->registerPlugin(new Application_Plugin_ViewPlugin());
return $autoloader;
}
if i wanted to have my app load profile names, like this: www.mydomain.com/simon where simon isnt a controller, its the username of the user to bring up the profile, is this possible?
class ProfileController extends Zend_Controller_Action {
public function init() {
}
public function indexAction(){
echo $this->_request->getParam('profileuser');
this is where i can display the user.
}
or something...
You might use Zend_Route for this purpose.
Just add the following to your application Bootstrap:
protected function _initRoutes()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute('profile', new Zend_Controller_Router_Route(
':profileuser',
array('module' => 'default', 'controller' => 'profile', 'action' => 'index')
));
}
More information on using Zend_Controller_Router here.
Presuming ProfileController is your default Controller, so you will get there by default, you can use
$profileName = ltrim($this->getRequest()->getRequestUri(), "/");
in indexAction to get 'simon' from your url.
I have a large list of old routes that I need to redirect to new routes.
I am already defining my custom routes in the Bootstrap:
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$oldRoute = 'old/route.html';
$newRoute = 'new/route/*';
//how do I add a 301 redirect to the new route?
$router->addRoute('new_route',
new Zend_Controller_Router_Route($newRoute,
array('controller' =>'fancy', 'action' => 'route')
));
}
How can I add routes that redirect the old routes using a 301 redirect to the new routes?
I've done it like this
Add a Zend_Route_Regexp route as the old route
Add Controller and action for the old route
Add logic to parse old route
Add $this->_redirect($url, array('code' => 301)) for this logic
Zend Framework does not have this type of functionality built in. So I have created a custom Route object in order to handle this:
class Zend_Controller_Router_Route_Redirect extends Zend_Controller_Router_Route
{
public function match($path, $partial = false)
{
if ($route = parent::match($path, $partial)) {
$helper = new Zend_Controller_Action_Helper_Redirector();
$helper->setCode(301);
$helper->gotoRoute($route);
}
}
}
Then you can use it when defining your routes:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initCustomRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route_Redirect('old/route/*', array('controller'=>'content', 'action'=>'index'));
$router->addRoute('old_route', $route);
}
}
In controller, try this way:
$this->getHelper('redirector')->setCode(301);
$this->_redirect(...);
There are a few approaches to solve this issue.
The easiest is probably to just use your .htaccess file to do a RewriteRule pattern substitution [R=301]
You could also detect what route was used in the controller and redirect based on that:
public function preDispatch() {
$router = $this->getFrontController()->getRouter();
if ($router->getCurrentRouteName() != 'default') {
return $this->_redirect($url, array('code'=>301));
}
}
The way I used to do it was to redirect to a controller that only handled redirects. Now I use the custom class mentioned in my other answer.
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
//new routes
$router->addRoute('myroute',
new Zend_Controller_Router_Route_Static('/new/route/1234',
array('controller' =>'brands', 'action' => 'view', 'id' => '4')
));
//old routes
$oldRoutes = array(
'/old/route/number/1' => '/new/route/1234',
}
foreach ($oldRoutes as $oldRoute => $newRoute) {
$router->addRoute($oldRoute, new Zend_Controller_Router_Route_Static($oldRoute, array('controller' =>'old-routes', 'action' => 'redirect', 'new-route' => $newRoute)));
}
}
}
And the controller:
class OldRoutesController extends Zend_Controller_Action
{
public function redirectAction()
{
$newRoute = $this->_getParam('new-route');
return $this->_redirect($newRoute, array('code' => 301));
}
}
Background information:
I'm in my admin module, and I created a view helper in modules/admin/views/helpers/AdminPanel.php. I have a layout plugin that forces my view to use the layout in admin/views/layouts/default.phtml.
I'm trying to access my ACL object to determine whether or not the user has resources in the context of a view helper, and then determine whether to return the admin panel html by parsing a configs/admin-nav.xml or not return anything at all.
I'm calling it in my admin layout like so:
<?php echo $this->AdminPanel(); ?>
And the class code which is blank, which I need to access the acl object in:
class My_View_Helper_AdminPanel extends Zend_View_Helper_Abstract {
public function AdminPanel() {}
}
I tried this:
$acl = Zend_Controller_Action_HelperBroker::getStaticHelper('acl');
But this probably isn't what I'm looking for as it forces the default module's views/layouts/default.phtml to load and errors occur.
Here's my global bootstrap file:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
private $_acl = null;
private $_auth = null;
protected function _initDoctype() {
$this->bootstrap('view');
$view = $this->getResource('view');
$view->setEncoding('UTF-8');
$view->doctype('HTML4_STRICT');
}
protected function _initAutoload() {
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('KG_');
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form_'
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
)
)
));
return $autoloader;
}
protected function _initAclAuth() {
$this->_acl = new Model_Acl;
$this->_auth = Zend_Auth::getInstance();
}
protected function _initNav() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$config = new Zend_Config_Xml( APPLICATION_PATH . '/configs/nav.xml', 'mainNav');
$navigation = new Zend_Navigation( $config );
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin( new KG_Controller_Plugin_Acl( $this->_acl, $this->_auth ) );
$role = $this->_auth->getStorage()->read()->role;
if ( !$role ) {
$role = 'guest';
}
$view->navigation( $navigation )->setAcl( $this->_acl)->setRole( $role );
}
protected function _initEncoding() {
$fc = Zend_Controller_Front::getInstance();
$response = new Zend_Controller_Response_Http;
$response->setHeader('Content-Type','text/html;charset=utf-8', true);
$fc->setResponse($response);
}
}
Your KG_Controller_Plugin_Acl plugin should be taking care of the access logic, not your view. If the user doesn't have access to the resource, you should error out or redirect the user to somewhere else.
The layout should be set within the controller. Preferably in the init() method with:
$this->_helper->layout->setLayout();
It looks like you followed the same or a similar tutorial as I did for Zend_Acl. I'm posting my plugin for reference, which includes the logic to handle access control from within the plugin:
class App_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
protected $_auth = null;
protected $_acl = null;
public function __construct(Zend_Auth $auth, Zend_Acl $acl)
{
$this->_auth = $auth;
$this->_acl = $acl;
}
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if ($this->_auth->hasIdentity()) {
$identity = $this->_auth->getIdentity();
$role = $identity->acl_role;
} else {
$role = 'guest';
}
// Mapping to determine which Resource the current
// request refers to (really simple for this example!)
$resource = $request->controller;
$privilege = $request->action;
if (!$this->_acl->has($resource)) {
$resource = null;
}
// ACL Access Check
if (!$this->_acl->isAllowed($role, $resource, $privilege)) {
if ($this->_auth->hasIdentity()) {
// authenticated, denied access, forward to /error/permissions
$request->setModuleName('default');
$request->setControllerName('error');
$request->setActionName('permissions');
} else {
// not authenticated, forward to login form
$request->setModuleName('default');
$request->setControllerName('auth');
$request->setActionName('login');
}
}
}
}