Different Menu for different modules - zend-framework2
every one. I am very new to ZendFramework2. I have a simplest project in which I have two modules--- users module and admin module. User module has login and registration and admin module has profile and logout in its menu. I want to have different menus for admin and users module. I used Navigation and make menu for admin in its module.config.php which works correctly. I also used different layouts for both users and admin modules in their view. But when I try to use navigation for users module, it access navigation of admin module.
Please help me, because I have no teacher and no supporter except you great people and viewer.
My code is given below:
module.config.php for Users module
<?php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Register' => 'Users\Controller\RegisterController',
'Users\Controller\Login' => 'Users\Controller\LoginController',
),
),
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
'route' => '/users',
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'login' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/login[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'index',
),
),
),
'register' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/register[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Register',
'action' => 'index',
),
),
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
'view_manager' => array(
'template_map' => array(
'layout/layout_users' => __DIR__ . '/../view/layout/layout.phtml',
),
'template_path_stack' => array(
'users' => __DIR__ . '/../view',
),
),
'navigation' => array(
'default' => array(
array(
'label' => 'Login',
'route' => 'login',
),
array(
'label' => 'Register',
'route' => 'register',
),
),
),
);
layout.phtml for users module:
<?php echo $this->doctype(); ?>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo $this->headTitle('ZF2 '. $this->translate('Transport Information System'))->setSeparator(' - ')->setAutoEscape(false) ?>
<?php echo $this->headMeta()
->appendName('viewport', 'width=device-width, initial-scale=1.0')
->appendHttpEquiv('X-UA-Compatible', 'IE=edge')
?>
<!-- Le styles -->
<?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
->prependStylesheet($this->basePath('css/style.css'))
->prependStylesheet($this->basePath('css/bootstrap-theme.min.css'))
->prependStylesheet($this->basePath('css/bootstrap.min.css')) ?>
<!-- Scripts -->
<?php echo $this->headScript()
->prependFile($this->basePath('js/bootstrap.min.js'))
->prependFile($this->basePath('js/jquery.min.js'))
->prependFile($this->basePath('js/respond.min.js'), 'text/javascript', array('conditional' => 'lt IE 9',))
->prependFile($this->basePath('js/html5shiv.min.js'), 'text/javascript', array('conditional' => 'lt IE 9',))
; ?>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="<?php echo $this->url('home') ?>"><img src="<?php echo $this->basePath('img/zf2-logo.png') ?>" alt="Zend Framework 2"/> <?php echo $this->translate('Transport Information System') ?></a>
</div>
<div class="collapse navbar-collapse">
<?php
echo $this->navigation('navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setUlClass('nav navbar-nav');
?>
</div>
</div>
</nav>
<div class="container">
<?php echo $this->content; ?>
<hr>
<footer>
<p align="center">Copyright will be updated very soon InshaAllah#<?php echo $this->translate('All rights reserved.') ?></p>
</footer>
</div> <!-- /container -->
<?php echo $this->inlineScript() ?>
</body>
module.config.php for Admin module:
<?php
namespace Admin;
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Admin\Controller\Index',
'action' => 'index',
),
),
),
'profile' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/profile[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Admin\Controller\Profile',
'action' => 'index',
),
),
),
'logout' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/users/logout',
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'logout',
),
),
),
'admin' => array(
'type' => 'Literal',
'options' => array(
'route' => '/admin',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
// 'Admin\Controller\Index' => Controller\IndexController::class
'Admin\Controller\Index' => 'Admin\Controller\IndexController',
'Admin\Controller\Profile' => 'Admin\Controller\ProfileController',
'Admin\Controller\Provinces' => 'Admin\Controller\ProvincesController',
'Admin\Controller\Districts' => 'Admin\Controller\DistrictsController',
'Admin\Controller\Cities' => 'Admin\Controller\CitiesController',
'Admin\Controller\Stations' => 'Admin\Controller\StationsController',
'Admin\Controller\Services' => 'Admin\Controller\ServicesController',
'Admin\Controller\Vehicles' => 'Admin\Controller\VehiclesController',
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout_admin' => __DIR__ . '/../view/layout/layout.phtml',
'admin/index/index' => __DIR__ . '/../view/admin/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'Profile',
'route' => 'profile',
),
array(
'label' => 'Logout',
'route' => 'logout',
),
),
),
);
layout.phtml for admin module:
<?php echo $this->doctype(); ?>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo $this->headTitle('ZF2 '. $this->translate('Transport Information System'))->setSeparator(' - ')->setAutoEscape(false) ?>
<?php echo $this->headMeta()
->appendName('viewport', 'width=device-width, initial-scale=1.0')
->appendHttpEquiv('X-UA-Compatible', 'IE=edge')
?>
<!-- Le styles -->
<?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
->prependStylesheet($this->basePath('css/style.css'))
->prependStylesheet($this->basePath('css/bootstrap-theme.min.css'))
->prependStylesheet($this->basePath('css/bootstrap.min.css')) ?>
<!-- Scripts -->
<?php echo $this->headScript()
->prependFile($this->basePath('js/bootstrap.min.js'))
->prependFile($this->basePath('js/jquery.min.js'))
->prependFile($this->basePath('js/respond.min.js'), 'text/javascript', array('conditional' => 'lt IE 9',))
->prependFile($this->basePath('js/html5shiv.min.js'), 'text/javascript', array('conditional' => 'lt IE 9',))
; ?>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="<?php echo $this->url('home') ?>"><img src="<?php echo $this->basePath('img/zf2-logo.png') ?>" alt="Zend Framework 2"/> <?php echo $this->translate('Transport Information System') ?></a>
</div>
<div class="collapse navbar-collapse">
<?php // <-- Update this !!
echo $this->navigation('navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setUlClass('nav navbar-nav');
?>
</div>
</div>
</nav>
<div class="container">
<?php echo $this->content; ?>
<hr>
<footer>
<p align="center">Copyright will be updated very soon InshaAllah.#<?php echo $this->translate('All rights reserved.') ?></p>
</footer>
</div> <!-- /container -->
<?php echo $this->inlineScript() ?>
</body>
I am looking at my code as I have an admin navigation menu.
In your admin module you will need the following file which I have saved in Zend\Navigation\Service folder.
namespace Admin\Zend\Navigation\Service;
use Zend\Navigation\Service\DefaultNavigationFactory;
class AdminNavigationFactory extends DefaultNavigationFactory
{
protected function getName()
{
return 'admin_navigation';
}
}
In your module.config you will need.
'navigation' => array(
'admin_navigation' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'Profile',
'route' => 'profile',
),
array(
'label' => 'Logout',
'route' => 'logout',
),
),
),
And finally in your admin module layout.phtml file you will need.
echo $this->navigation('admin_navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setUlClass('nav navbar-nav');
Hopefully this will get it working for you.
You might also want to watch this Zend Framework 2 Secondary Navigation With Acl
Related
zf2 breadcrumbs - child page
I have a problem with the configuration of bradcrumbs of my application. I begin by report configuration for my module : 'corso' => array( 'type' => 'segment', 'options' => array( 'route' => '/corso[/:action[/:id]]', 'constraints' => array( 'id' => '[0-9]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]+'), 'defaults' => array( 'controller' => 'corso/Controller/corso', 'action' => 'index', ), ), ), The controller has the usual methods index, add and edit action for basic crud operation As you can see from the configuration file add and edit actions are not child route. I would like to get a bradcrumbs of this type home> index action controller> add / edit action the first and second elements are links. this is partial view of breadcrumbs <li><i class="iconfa-home"></i> <span class="separator"></span></li> <?php $active = $container->findActive($navigation) ?> <?php foreach($this->pages as $page): ?> <?php /* #var $page \Zend\Navigation\Page\Mvc */ ?> <?php if( ! $page->isActive()): ?> <li> <?php echo $page->getLabel() ?> <span class="divider">/</span> </li> <?php else: ?> <li class="active"> <?php if($container->getLinkLast()): ?><a href="<?php echo $page->getHref() ?>"><?php endif ?> <?php echo $page->getLabel() ?> <?php if($container->getLinkLast()): ?><a href="<?php echo $page->getHref() ?>"><?php endif ?> </li> <?php endif ?> <?php endforeach ?> What should I add? 'navigation' => array( 'default' => array( array( 'label' => 'corso', 'route' => 'corso', 'action' => 'index' ), ----- ), ), thanks edit: i use zf2 helper for bredcrumbs $this->navigation('navigation')->breadcrumbs()->setMinDepth(0)->setPartial(array('partial/breadcrumb', 'corso'));
There is example in Zend\Navigation Quick Start for nested pages: http://framework.zend.com/manual/2.3/en/modules/zend.navigation.quick-start.html#quick-start In your case, it should be like this: 'navigation' => array( 'default' => array( array( 'label' => 'corso', 'route' => 'corso', 'action' => 'index', 'pages' => array( array( 'label' => 'add action ', 'route' => 'corso', 'action' => 'add', ), array( 'label' => 'edit action ', 'route' => 'corso', 'action' => 'edit', ), ), ), ), ),
Adding a new controller in ZF2 throws 404 not found error
I have added a new usersmanagement controller in ZF2.If i execute the controller(http://localhost/app/usersmanagement/).It shows 404 page not found error. Can anyone tell me what will be the issue, Below is my module.config.php configuration , <?php return array( 'router' => array( 'routes' => array( 'home' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Application\Controller\Usersmanagement', 'action' => 'index', ), ), ), 'usersmanagement' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => 'usersmanagement', 'defaults' => array( 'controller' => 'Application\Controller\Usersmanagement', 'action' => 'index', ), ), ), // The following is a route to simplify getting started creating // new controllers and actions without needing to create a new // module. Simply drop new controllers in, and you can access them // using the path /application/:controller/:action 'application' => array( 'type' => 'Literal', 'options' => array( 'route' => '/application', 'defaults' => array( '__NAMESPACE__' => 'Application\Controller', 'controller' => 'Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( ), ), ), ), ), ), ), 'service_manager' => array( 'abstract_factories' => array( 'Zend\Cache\Service\StorageCacheAbstractServiceFactory', 'Zend\Log\LoggerAbstractServiceFactory', ), 'aliases' => array( 'translator' => 'MvcTranslator', ), ), 'translator' => array( 'locale' => 'en_US', 'translation_file_patterns' => array( array( 'type' => 'gettext', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.mo', ), ), ), 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController', 'Application\Controller\Usersmanagement' => 'Application\Controller\UsersmanagementController' ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', ), ), // Placeholder for console routes 'console' => array( 'router' => array( 'routes' => array( ), ), ), ); Thanks
First, please make sure you have an indexAction method on your Application\Controller\Usersmanagement Controller. As #Microbe pointed out, you must correct the line 'route' => 'usersmanagement', to 'route' => '/usersmanagement', There is another problem with your module.config.php. The home route points to the Application\Controller\Usersmanagement controller. In this case both URLs ('/' and '/usersmanagement') will call the same action Application\Controller\Usersmanagement::indexAction() and therefore get the same result.
Zend Framework 2 url helper
I am wondering why there is no id parameter shown. The generated URL is: 'http://example.org/editroom' but I expected 'http://example.org/editroom/2'. What did I miss? Controller-code: echo "<a href='" . $this->url('home', array('action' => 'editroom', 'id' => $room->getId())) . "' title='Bearbeiten des Eintrags'>Bearbeiten</a>;";
You probably didn't define the id parameter in the home route. You need to add a child route to allow additional parameters: 'home' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '/[:controller[/[:action]]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'Application\Controller\IndexController', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'wildcard' => array( 'type' => 'Zend\Mvc\Router\Http\Wildcard', 'options' => array( 'key_value_delimiter' => '/', 'param_delimiter' => '/', ), ), ), ) Then you call it like this: echo "<a href='" . $this->url('home/wildcard', array('action' => 'editroom', 'id' => $room->getId())) . "' title='Bearbeiten des Eintrags'>Bearbeiten</a>;";
zf2&zfc2 and routing
I want to create admin panel using ZfcAdmin module. I want to create routing, to manage users. Here is it: <?php return array( 'controllers' => array( 'invokables' => array( 'AdminUser\Controller\AdminUser' => 'AdminUser\Controller\AdminUserController', ), ), 'view_manager' => array( 'template_path_stack' => array( 'admin-user' => __DIR__ . '/../view', ), ), 'router' => array( 'routes' => array( 'zfcadmin' => array( 'may_terminate' => true, 'child_routes' => array( 'user' => array( 'type' => 'segment', 'options' => array( 'route' => '/user', 'defaults' => array( 'controller' => 'AdminUser\Controller\AdminUser', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' =>array( 'edit' => array( 'type' => 'segment', 'options' => array( 'route' => '/edit/:user_id', 'defaults' => array( 'controller' => 'AdminUser\Controller\AdminUser', 'action' => 'edit', ), ), ), ), ), ), ), ), ), ); I based on info from zfcadmin github page: https://github.com/ZF-Commons/ZfcAdmin. I just copy and paste the example, and change to fix my needs. However, I recieve a error message: "Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\RuntimeException' with message 'Part route may not terminate'" What's wrong? EDIT: I request: /admin/user and it's ok, but when I want to receive URL like: /admin/user/edit/1 I always get /admin/user I create the link this way: <?php $this->url('zfcadmin/user/edit', array( 'action' => 'edit', 'user_id' => $user['user_id'], )) ?>
You creating URL incorrectly. Please create the link like this. <?php $this->url('zfcadmin/user/edit', array( 'user_id' => $user['user_id'], ) ?>
zend framework 2 (zfcadmin) and routing
I want to create admin panel using ZfcAdmin module. I want to create routing, to manage users. Here is it: <?php return array( 'controllers' => array( 'invokables' => array( 'AdminUser\Controller\AdminUser' => 'AdminUser\Controller\AdminUserController', ), ), 'view_manager' => array( 'template_path_stack' => array( 'admin-user' => __DIR__ . '/../view', ), ), 'router' => array( 'routes' => array( 'zfcadmin' => array( 'may_terminate' => true, 'child_routes' => array( 'user' => array( 'type' => 'segment', 'options' => array( 'route' => '/user', 'defaults' => array( 'controller' => 'AdminUser\Controller\AdminUser', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' =>array( 'edit' => array( 'type' => 'segment', 'options' => array( 'route' => '/edit/:user_id', 'defaults' => array( 'controller' => 'AdminUser\Controller\AdminUser', 'action' => 'edit', ), ), ), ), ), ), ), ), ), ); I request: /admin/user and it's ok, but when I want to receive URL like: /admin/user/edit/1 I always get /admin/user I create the link this way: <?php $this->url('zfcadmin/user/edit', array( 'action' => 'edit', 'user_id' => $user['user_id'], )) ?> why? what's wrong?
The first argument in the ZF2 Url view helper is a $name. Have you tried using 'edit' like you've specified in your config? I also think you may need to echo the value. <?php echo $this->url('edit', array('action' => 'edit', 'user_id' => $user['user_id'],)); ?>