Zend Framework 2 : Dynamic Navigation using Zend\Navigation
Zend Framework 2 provide Navigation component to generate menu. It can utilize by creating static config in autoload/*.global.php. Sometime, we need dynamic menu to be generated in our application. I will give you a simple example to do that.
For example, i want structure menu like this :
So, we should do step by step like the following :
1. Create a menu Table (I’m using postgresql, you can specify yourself for your database )
CREATE TABLE menu ( id bigserial NOT NULL, name character varying(255), label character varying(255), route character varying(255), CONSTRAINT pk_menu PRIMARY KEY (id ) ) WITH ( OIDS=FALSE ); ALTER TABLE menu OWNER TO postgres;
2. Insert the data like the following :
3. create a Model :
namespace ZendSkeletonModule\Model; use Zend\Db\Adapter\Adapter; use Zend\Db\ResultSet\HydratingResultSet; use Zend\Db\TableGateway\AbstractTableGateway; use Zend\Db\Sql\Select; use Zend\Db\Adapter\AdapterAwareInterface; class MenuTable extends AbstractTableGateway implements AdapterAwareInterface { protected $table = 'menu'; public function setDbAdapter(Adapter $adapter) { $this->adapter = $adapter; $this->resultSetPrototype = new HydratingResultSet(); $this->initialize(); } public function fetchAll() { $resultSet = $this->select(function (Select $select){ $select->order(array('id asc')); }); $resultSet = $resultSet->toArray(); return $resultSet; } }
4. Extends Zend\Navigation\Service\DefaultNavigationFactory and override getPages() function.
namespace ZendSkeletonModule\Navigation; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Navigation\Service\DefaultNavigationFactory; class MyNavigation extends DefaultNavigationFactory { protected function getPages(ServiceLocatorInterface $serviceLocator) { if (null === $this->pages) { //FETCH data from table menu : $fetchMenu = $serviceLocator->get('menu')->fetchAll(); foreach($fetchMenu as $key=>$row) { $configuration['navigation'][$this->getName()][$row['name']] = array( 'label' => $row['label'], 'route' => $row['route'], ); } if (!isset($configuration['navigation'])) { throw new Exception\InvalidArgumentException('Could not find navigation configuration key'); } if (!isset($configuration['navigation'][$this->getName()])) { throw new Exception\InvalidArgumentException(sprintf( 'Failed to find a navigation container by the name "%s"', $this->getName() )); } $application = $serviceLocator->get('Application'); $routeMatch = $application->getMvcEvent()->getRouteMatch(); $router = $application->getMvcEvent()->getRouter(); $pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]); $this->pages = $this->injectComponents($pages, $routeMatch, $router); } return $this->pages; } }
5. Create Your Navigation Factory
namespace ZendSkeletonModule\Navigation; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class MyNavigationFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { $navigation = new MyNavigation(); return $navigation->createService($serviceLocator); } }
6. Register MenuTable model, and your Navigation in ServiceManager :
class Module { public function getServiceConfig() { return array( 'initializers' => array( function ($instance, $sm) { if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) { $instance->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter')); } } ), 'invokables' => array( 'menu' => 'ZendSkeletonModule\Model\MenuTable' ), 'factories' => array( 'Navigation' => 'ZendSkeletonModule\Navigation\MyNavigationFactory' ) ); } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } public function getAutoloaderConfig() { return array( 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); } }
6. Configuration done, let’s call from layout:
<div class="nav-collapse"> <?php echo $this->navigation('Navigation')->menu()->setUlClass('nav'); ?> </div>
Done !
97 Responses
Subscribe to comments with RSS.
Excelente, Very good
Tank you very much, I appreciate.
You’re welcome
[…] https://samsonasik.wordpress.com/2012/11/18/zend-framework-2-dynamic-navigation-using-zend-navigation… […]
Really good examples, would be great to get examples on GitHub as well
You can follow my github account : https://github.com/samsonasik
You might take a look at ensemble as well (http://ensemble.github.com). The kernel (https://github.com/ensemble/EnsembleKernel) does exactly this, together with two more options:
1. Navigation can be a tree and not only a flat structure
2. Like the tree structure of the navigation component, routes can be hierarchical as well
The kernel implements an adapter pattern where currently only a doctrine one exists, but it’s easy to make one for Zend\Db as well.
great!, my post is just a sample to who are need simple implementation of dynamic navigation.
I apologize if too bold but may I suggest another blog post: Multi step form
thanks for the suggestion.
[…] Zend Framework 2 : Dynamic Navigation using ZendNavigation Автор: Abdul Malik Ikhsan Перевод: Лобач […]
it would have been great if you explained the code ..
not in details but few lines below each block explaining what is was doing.
Must have really helped the beginners. Anyways nice post indeed .. 🙂
HJ samsonasik!
has error how to fix?
Fatal error: Uncaught exception ‘Zend\Db\TableGateway\Exception\RuntimeException’ with message ‘This table does not have an Adapter setup’ in E:\webproject\ZendFramework2\library\Zend\Db\TableGateway\AbstractTableGateway.php:104 Stack trace: #0 E:\webproject\ZendFramework2\library\Zend\Db\TableGateway\AbstractTableGateway.php(187): Zend\Db\TableGateway\AbstractTableGateway->initialize() #1 E:\webproject\htdocs\zf2shopcms\backend\Admin\src\Admin\Model\MenuTable.php(27): Zend\Db\TableGateway\AbstractTableGateway->select(Object(Closure)) #2 E:\webproject\htdocs\zf2shopcms\backend\Admin\src\Admin\Navigation\MyNavigation.php(13): Admin\Model\MenuTable->fetchAll() #3 E:\webproject\ZendFramework2\library\Zend\Navigation\Service\AbstractNavigationFactory.php(40): Admin\Navigation\MyNavigation->getPages(Object(Zend\ServiceManager\ServiceManager)) #4 E:\webproject\htdocs\zf2shopcms\backend\Admin\src\Admin\Navigation\MyNavigationFactory.php(12): Zend\Navigation\Service\AbstractNavigationFactory->createService(Object(Zend\ServiceMana in E:\webproject\ZendFramework2\library\Zend\ServiceManager\ServiceManager.php on line 733
You have to set adapter first.
thank you for support!
i’m write getServiceConfig() in module.php file:
public function getServiceConfig()
{
return array(
‘factories’ => array(
‘Admin\Model\Users’ => function($sm) {
$dbAdapter = $sm->get(‘Zend\Db\Adapter\Adapter’);
$table = new Users($dbAdapter);
return $table;
},
‘Admin\Model\GroupUsers’ => function($sm) {
$dbAdapter = $sm->get(‘Zend\Db\Adapter\Adapter’);
$table = new GroupUsers($dbAdapter);
return $table;
},
),
// menu config
‘invokables’ => array(
‘menu’ => ‘Admin\Model\MenuTable’
),
‘factories’ => array(
‘Navigation’ => ‘Admin\Navigation\MyNavigationFactory’
)
);
}
code remaining write like you!
i’m not understand code is not running
sorry english me not good 😀
You should set adapter first. please take a look zf2 manual : http://zf2.readthedocs.org/en/latest/user-guide/database-and-models.html
Hi, has you work with zend framework 2 and postgres database with multiple schemas?, i’m using zf2 v2.0.6 but always add double quotes to the schema.table name, i llok in the zf2 issue tracker but i dont see nothing like this. i’m using pdo driver to connect.
in setDbAdapter, re-define the $table with TableIdentifier :
Thanks, i’m looking for this, is posible to implement in the AuthAdapter?, i can’t believe that this issue isn’t supported like zf1 :(.
feel free to contribute to zf2 : https://github.com/zendframework/zf2/
Hi, very good tutorial, but I have now problem adding a second navigation bar. Is it posible to do that when yes how, I have read this but dosn’t help.
http://stackoverflow.com/questions/12972316/how-to-set-up-2-navigations-in-zf2
maybe, you should take a look Jurian Sluiman AssembleKernel : https://github.com/ensemble/EnsembleKernel
hey man your all tutorials are wonderfull. I followed your blog but i’m getting on issue that the address of all links showing only to home(www.weblogz2.loc). what could be the possible reason. please help me to rectify this issue.
check your module.config.php on route section, try with Literal first instead of Segment.
I’m really very thankful to you for reply, you are great instructor. This is my module.config.php file address http://pastie.org/6098043 Please if possible have a look of it and suggest me where i have to change for proper link address. i’m really messed up with this thing. if you can, then please mail me the changed file.
with your config. your call-ing route in navigation should be ‘album’ . and it should be work.
Hi, I’ve just attempted this tutorial and It seems the Navigation factory never gets called. I put some debug in the factory and nothing prints. I’m using the latest version of Zend Framework 2, is this still the correct way to do it? Code is as follows: https://gist.github.com/anonymous/5034200
The var_dump in DefaultNavigationFactory never happens.
NM, I got it to work.
I ve got this problem but not get the solution …. What is your solution ?
Hi Samsonasik,
I’m new in ZF2 (2.1.3).
I follow entire step provided by you. But, I’m getting following error:
( ! ) Fatal error: Uncaught exception ‘Zend\Navigation\Exception\InvalidArgumentException’ with message ‘Invalid argument: Unable to determine class to instantiate’ in D:\wamp\www\zend\2.x\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 744
( ! ) Zend\Navigation\Exception\InvalidArgumentException: Invalid argument: Unable to determine class to instantiate in D:\wamp\www\zend\2.x\vendor\zendframework\zendframework\library\Zend\Navigation\Page\AbstractPage.php on line 225
Thanks
Bharat
add the following common functions to your module class :
and don’t forget to set your db adapter. read the docs http://zf2.readthedocs.org/en/latest/user-guide/database-and-models.html
Hello, samsonasik!
I follow entire step provided by you and I’m getting error:
Fatal error: Uncaught exception ‘Zend\ServiceManager\Exception\ServiceNotCreatedException’ with message ‘While attempting to create navigation(alias: Navigation) an invalid factory was registered for this instance type.’ in W:\domains\lp.loc\vendor\ZF2\library\Zend\ServiceManager\ServiceManager.php:871
Thanks!
you should be aware with namespace you used.
Hi!
Same trouble.
Did you solve it?
Thanks.
Hi, very nice code!!! how can I get the current or active module from here? thank you!!!
I did it like this: $routeMatch->getMatchedRouteName();
I have other question, if I want to invocate a function like this:
echo $this->navigation(‘MyNavigation’)->testFunction();
from layout.phtml, how can I did it?
Thanks again!
i don’t think so, don’t use it even possible, use view_helpers!
i don’t think so. use view_helpers !
i don’t think so, use view_helpers to do something like that !
Hi, great code… helps a lot.
But, I am wondering on how to make it work on two separate menus (default and admin) in a fancy way (with submenu and icons).
I have made it work on a static way.
you can add class/id or/and other attributes beside of label and route.
hi sam.
could you show me how to use BREADCRUMBS by navigation . thanks a lot !!
read the docs http://zf2.readthedocs.org/en/latest/tutorials/tutorial.navigation.html
thank you for your help .
I did it follow the docs , I have made it works,
but the breadcrumbs seems always “Album > Add” whatever the page i visit.
how can i get the true breadcrumbs ?
it’s buggy in 2.1.5, update your zf lib to the latest dev master : https://github.com/zendframework/zf2
Awesome! Great work mate, keep it up!
1 more thing, if you get a chance or you have free time maybe you can add another feature. Like implementing the BjyAuthorize(ACL) in navigation. It would be a great help for all beginners like me. Again, Thank you for this helpful blog!
good idea, thanks.
Hi Samsonasik
i have two questions for you.
i want to add a search item to a navigation from configuration. do you know if this is possible?
So instead of a item i want to have a input field for a search action.
the second question:
can i create a navigation/ menu from a module and just display it on specific pages within that module?
I want to implement a “show step of a process” bar, that the user knows on which step it is currently working…
like when you get to the order process within a shop system, you always get a bar displayed, where you can see on which step you are currently (select paymentmethod, confirm the order, finish payment)
i hope my english is not too bad and the questions are enough detailed 🙂
Hi Samsonasik,
i have one questions for you. how can pass extra parameters on fatchAll($data);
Because i need to condition base data fatch
protected function getPages(ServiceLocatorInterface $serviceLocator)
{
if (null === $this->pages) {
//FETCH data from table menu :
$fetchMenu = $serviceLocator->get(‘menu’)->fetchAll();
$configuration[‘navigation’][$this->getName()] = array();
foreach($fetchMenu as $key=>$row)
{
$configuration[‘navigation’][$this->getName()][$row[‘name’]] = array(
‘label’ => $row[‘label’],
‘route’ => $row[‘route’],
);
}
if (!isset($configuration[‘navigation’])) {
throw new Exception\InvalidArgumentException(‘Could not find navigation configuration key’);
}
if (!isset($configuration[‘navigation’][$this->getName()])) {
throw new Exception\InvalidArgumentException(sprintf(
‘Failed to find a navigation container by the name “%s”‘,
$this->getName()
));
}
$application = $serviceLocator->get(‘Application’);
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$pages = $this->getPagesFromConfig($configuration[‘navigation’][$this->getName()]);
$this->pages = $this->injectComponents($pages, $routeMatch, $router);
}
return $this->pages;
}
$fetchMenu = $serviceLocator->get(‘menu’)->fetchAll(‘How can pass the condition data like(array)’);
Please can you help me 🙂
if conditional parameter is segment parameter, you can do this :
and just modify your fetchAll($param) code at TableModel :
hi
I am trying to get the navigation for main category and sub category… Could u please help me
http://zf2.readthedocs.org/en/latest/modules/zend.navigation.quick-start.html
Uncaught exception ‘Zend\ServiceManager\Exception\ServiceNotCreatedException’ with message ‘While attempting to create navigation(alias: Navigation) an invalid factory was registered for this instance type.’ in C:\wamp\www\nav\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 1031
will u clear this issue,
Uncaught exception ‘Zend\ServiceManager\Exception\ServiceNotFoundException’ with message ‘Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter’ in C:\wamp\www\nav\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 518
if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) {
$instance->setDbAdapter($sm->get(‘Zend\Db\Adapter\Adapter’));
}
check ur factories whether in local or module…
hai sam,
good post,
for submenu I coded in mod.cong.php $pages=array(….);but it not works
hi, i’m try to develeop navigation in zf2 to use of container and uri. i got fatel error generating that has
Fatal error: Uncaught exception ‘Zend\Navigation\Exception\InvalidArgumentException’ with message ‘Could not find navigation configuration key’ in C:\Apache\htdocs\SMBERP\vendor\zendframework\zendframework\library\Zend\Navigation\Service\AbstractNavigationFactory.php:57 Stack trace: #0 C:\Apache\htdocs\SMBERP\vendor\zendframework\zendframework\library\Zend\Navigation\Service\AbstractNavigationFactory.php(36): Zend\Navigation\Service\AbstractNavigationFactory->getPages(Object(Zend\ServiceManager\ServiceManager)) #1 [internal function]: Zend\Navigation\Service\AbstractNavigationFactory->createService(Object(Zend\ServiceManager\ServiceManager), ‘navigation’, ‘navigation’) #2 C:\Apache\htdocs\SMBERP\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(852): call_user_func(Array, Object(Zend\ServiceManager\ServiceManager), ‘navigation’, ‘navigation’) #3 C:\Apache\htdocs\SMBERP\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(982): Zend\ServiceManager\ServiceManager- in C:\Apache\htdocs\SMBERP\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 859
wat i have did wrong? plz give me a detailed explanation for how to use container with uri….
i got menu but i want to seperate datas from database by aline datas in ULClass.give me a details….
sry for bad english……
Uncaught exception ‘Zend\Db\TableGateway\Exception\InvalidArgumentException’ with message ‘Invalid method (_get) called, caught by Zend\Db\TableGateway\AbstractTableGateway::__call()
my codings
public function fetchAll()
{
$resultSet = $this->_get(function($select){
$select=”SELECT*
FROM menu AS parent
LEFT JOIN menu AS child
ON child.matches = parent.id
WHERE parent.id
ORDER BY parent.id, child.id”;
// $select->where(array(‘id =1’));
});
$resultSet = $resultSet->toArray();
return $resultSet;
}
any idea,,,,,,,,
Commenting again because html html data not showing.
Using navigation in zend create html like this
“Menu”
but If i want data like
“Menu“
Html data is not showing here again.By the way my question is if i want to add a span tag before achar tag then what i have to do?
http://en.support.wordpress.com/code/posting-source-code/
http://lmgtfy.com/?q=zf2+navigation+add+span
oh man, google before ask!
Thanks for tips. Now, I want to add param to navigation, example, article id, category alias….. So how to do?
character varying nya apa harus 225 semua ya
ga
Hi samsonasik,
Do you have any separate tutorial on creating a complete DB based ACL Navigation ?
I m trying to create one but it is having a lot of issues. Finally I m trying to start with this codeset and modify it. Any inputs will be welcomed.
I followed the steps above but getting this error –
[Thu Dec 19 00:52:36 2013] [error] [client xxx.xx.xxx.xxxPHP Fatal error: Uncaught exception ‘Zend\\ServiceManager\\Exception\\ServiceNotCreatedException’ with message ‘While attempting to create navigation(alias: navigation) an invalid factory was registered for this instance type.’ in /usr/local/project/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:1031\nStack trace:\n#0 /usr/local/project/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(601): Zend\\ServiceManager\\ServiceManager->createFromFactory(‘navigation’, ‘navigation’)\n#1 /usr/local/project/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(561): Zend\\ServiceManager\\ServiceManager->doCreate(‘navigation’, ‘navigation’)\n#2 /usr/local/project/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(503): Zend\\ServiceManager\\ServiceManager->create(Array)\n#3 /usr/local/project/vendor/zendframework/zendframework/library/Zend/View/Helper/Navigation/A in /usr/local/project/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 1031
Thanks in advance. You are doing a great job as a Teacher. I m already your followed on twitter
My requirement –
A user is part of multiple GROUPS (or ROLES).
I need to implement ACL which reads completely from Database and not from configuration file.
Is there any article you can help me with ?
Scenario:
User1 => Role1, Role2
User2 => Role2
User3 => Role3
Resource1 Access -> Role1
Resource2 Access -> Role1 and Role2
Resource3 Access -> Role3
Appropriately I need to render the Navigation Menu. Remember there may n number of sub menus.
hello . samsonasik !!
I misstake when add submenu. i wan’t like about example. but i can’t . your can do like about example or can do zend navigation contaner
Thanks for this code, samsonasik!
I found that when I implemented it, it didn’t render the way I wanted it to using the default bootstrap skin. The fix is to add the navbar-nav class to the layout.phtml statement.
Instead of:
navigation(‘Navigation’)->menu()->setUlClass(‘nav’); ?>
Use:
navigation(‘Navigation’)->menu()->setUlClass(‘nav navbar-nav’); ?>
Thanks again for this code, samsonasik!
Can you tell me if there is a way to pass options to the route variable (or to some other variable) in the container configuration? For instance, the ZF2 Album module tutorial suggests that “/album/edit/2” is the route to the edit page for id 2. However, in the basic module “/album/edit/2” is NOT the route: “album” is the route and “action” and “id” are options. If “/album/edit/2” is entered in the route field in the menu table, your code throws an error because “/edit/2” is not part of the route. Similarly, the example you’ve given in your sample database above won’t work for “SampleModule/default” unless the “/default” segment is set up as a child_route and not an action.
There are probably a number of solutions. One might be to reconfigure the router array in module.config.php to identify sub segments as child_routes instead of options (although I’m not sure how to inject an id variable as a route name). Another solution is to use the uri variable instead of the route variable in the table and in the container configuration. However, I would like to know if there’s a way of passing something like “/album?action=edit&id=2” through the route variable, or if there are other variables available to pass option metadata.
Thanks again for this code, samsonasik!
Is there a way to instruct the navigation container to generate the HTML for a dropdown menu?
I have forked your code in order to develop submenus and my new code looks something like this:
namespace Menu\Navigation;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;
class MyNavigation extends DefaultNavigationFactory
{
protected function getPages(ServiceLocatorInterface $serviceLocator)
{
if (null === $this->pages) {
//FETCH data from table menu :
$fetchMainMenu = $serviceLocator->get(‘menu’)->fetchMainMenu();
$configuration[‘navigation’][$this->getName()] = array();
foreach($fetchMainMenu as $MainMenuItem=>$MainMenuField)
{
$fetchSubMenu = $serviceLocator->get(‘menu’)->fetchSubMenu($MainMenuField[‘ml1’]);
$subMenu = array();
foreach($fetchSubMenu as $SubMenuItem=>$SubMenuField)
{
array_push($subMenu,
array(
‘label’ => $SubMenuField[‘label’],
‘uri’ => $SubMenuField[‘uri’],
‘title’ => $SubMenuField[‘title’],
‘active’ => false,
)
);
}
$configuration[‘navigation’][$this->getName()][$MainMenuField[‘name’]] = array(
‘label’ => $MainMenuField[‘label’],
‘uri’ => $MainMenuField[‘uri’],
‘pages’ => $subMenu,
‘title’ => $MainMenuField[‘title’],
‘active’ => false,
);
}
if (!isset($configuration[‘navigation’])) {
throw new Exception\InvalidArgumentException(‘Could not find navigation configuration key’);
}
if (!isset($configuration[‘navigation’][$this->getName()])) {
throw new Exception\InvalidArgumentException(sprintf(
‘Failed to find a navigation container by the name “%s”‘,
$this->getName()
));
}
$application = $serviceLocator->get(‘Application’);
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$pages = $this->getPagesFromConfig($configuration[‘navigation’][$this->getName()]);
$this->pages = $this->injectComponents($pages, $routeMatch, $router);
}
return $this->pages;
}
}
The code above generates the following HTML:
Home
Knowledgebase
page 1
page 2
page 3
However, for the dropdown menu to expand and collapse, we want to generate this code:
Home
Knowledgebase
page 1
page 2
page 3
Is there a way to pass instructions to identify the appropriate li’s as class=”dropdown”, the appropriate ul’s as class=”dropdown-menu ” and the appropriate a’s as class=” dropdown-toggle”; and to insert a symbol in the dropdown item tag?
Thanks.
Oops. WordPress converted the HMTL code in my comment. Here’s another try.
The code above generates the following HTML:
However, we want to generate this code:
Nope, that didn’t work either. Sorry, there’s not a function to allow me to delete or edit my comments. Please edit my comment so it renders as code.
i need examply with doctrine2
you need to buy me a coffee 🙂 https://samsonasik.wordpress.com/buy-me-a-coffee/
Hi samsonasik,
I´ve spent a few days trying to get this navigation working. I have read all the replys and a lot of forums but I still have a problem.
I have created the table on the database and the files MenuTable, MyNavigation, MyNavigationFactory under src/Application/Model.
The namespaces for each file are:
MenuTable: Application\Model
MyNavigation: Application\Navigation
MyNavigationFactory: Application\Navigation
I’ve modified Application/Module.php following your instructions with these namespaces:
public function getServiceConfig()
{
return array(
‘initializers’ => array(
function ($instance, $sm) {
if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) {
$instance->setDbAdapter($sm->get(‘Zend\Db\Adapter\Adapter’));
}
}
),
‘invokables’ => array(
‘menu’ => ‘Application\Model\MenuTable’
),
‘factories’ => array(
‘Navigation’ => ‘Application\Model\MyNavigationFactory’ /* here I have had to change the path to avoid uncaught exception errors */
)
);
}
At last, I have insrted the following lines for the layout:
navigation(‘Navigation’)->menu()->setUlClass(‘nav’); ?>
When I run the application, both the webpage and Z-Ray show me the following error:
Fatal error: Cannot redeclare class Application\Navigation\MyNavigationFactory in /usr/local/zend/apache2/htdocs/my_viena/module/Application/src/Application/Model/MyNavigationFactory.php on line 7
It seems like I have already declared this class…
Could you please help me with this issue? Is this a matter of namespaces? Did I create the files in the wrong folder?
I will really appreciate your help.
Thanks in advance,
Jairo García
In ZendSkeletonModule\Navigation\MyNavigation::getPages()
if (!isset($configuration[‘navigation’])) and
if (!isset($configuration[‘navigation’][$this->getName()]))
will never called, because in previous line you initialize this array:
$configuration[‘navigation’][$this->getName()] = array();
Thanks ;), I’ve fixed it by removing the initialization.
Thanks for this tutorial very helpful
I’m new to ZF
And I have a problem I do not understand.
When I set navigation to “route”
$configuration[‘navigation’][$this->getName()][$row[‘name’]] = array(
‘label’ => $row[‘label’],
‘route’ => $row[‘route’],
);
i get message
Fatal error: Zend\Mvc\Router\Exception\RuntimeException: Route with name “” not found in
When I set navigation to “uri” all work good
$configuration[‘navigation’][$this->getName()][$row[‘name’]] = array(
‘label’ => $row[‘label’],
‘uri’ => $row[‘route’],
);
Application router
‘router’ => array(
‘routes’ => array(
‘home’ => array(
‘type’ => ‘Literal’,
‘options’ => array(
‘route’ => ‘/’,
‘defaults’ => array(
‘controller’ => ‘Application\Controller\Index’,
‘action’ => ‘index’,
),
),
),
‘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(
),
),
),
),
),
),
),
check the data first of $fetchMenu with var_dump if data grabbed. and debug it.
I am project manager. m team built a web site in Zend. it conains a menu on left. when we click any item in menu, whole page gets reload. Can we make it that the menu stays there and the resulting page is loaded in a separate div instead?
yes, use ajax. it’s nothing todo with zf 😛
Hi samsonasik,
First let me thank you for sharing this post,
I followed the steps carefully and finally render the menu as expected, but also gives the following error:
C:\xampp\htdocs\outPruebas\vendor\zendframework\zendframework\library\Zend\Navigation\Page\AbstractPage.php:232
message:
Invalid argument: Unable to determine class to instantiate
I’ve tried with mvc and uri type pages.
I have run the project with debug and does not go through line 232, the variables $hasUri or $hasMvc are set correctly, and fails in method run () class MVC\Application.php
Thank you in advance.
I tried many times, and worked. You probably need to check your php version or other requirements.
Hi samsonasik,
how create a dynamic breadcrumbs using db or xml in zf2.
Thank you in advance.
what you’ve seen is using db, just change the value
I followed the steps and change the menu() function with breadcrumbs() but its not working as dynamic. it works as a default function of navigation only.
I will really appreciate your help.
Thanks in advance,
check the documentation http://zf2.readthedocs.org/en/latest/tutorials/tutorial.navigation.html ! , also, “not working” is not clear info to get help
How to solve this bug.
Fatal error: Uncaught exception ‘Zend\Loader\Exception\InvalidArgumentException’ with message ‘Map file provided does not exist. Map file: “C:\xampp\htdocs\new\module\Album/autoload_classmap.php”‘ in C:\xampp\htdocs\new\vendor\zendframework\zend-loader\src\ClassMapAutoloader.php:171 Stack trace: #0 C:\xampp\htdocs\new\vendor\zendframework\zend-loader\src\ClassMapAutoloader.php(81): Zend\Loader\ClassMapAutoloader->loadMapFromFile(‘C:\\xampp\\htdocs…’) #1 C:\xampp\htdocs\new\vendor\zendframework\zend-loader\src\ClassMapAutoloader.php(117): Zend\Loader\ClassMapAutoloader->registerAutoloadMap(‘C:\\xampp\\htdocs…’) #2 C:\xampp\htdocs\new\vendor\zendframework\zend-loader\src\ClassMapAutoloader.php(60): Zend\Loader\ClassMapAutoloader->registerAutoloadMaps(Array) #3 C:\xampp\htdocs\new\vendor\zendframework\zend-loader\src\ClassMapAutoloader.php(46): Zend\Loader\ClassMapAutoloader->setOptions(Array) #4 C:\xampp\htdocs\new\vendor\zendframework\zend-loader\src\AutoloaderFactory.php(99): Zend\Loader\ClassMapAutoloader->__construc in C:\xampp\htdocs\new\vendor\zendframework\zend-loader\src\ClassMapAutoloader.php on line 171
read the zf2 docs slowly … http://zf2.readthedocs.org/en/latest/user-guide/modules.html#autoloading-files
thanks sam
Hi samsonasik,
This article has been helping very much, but a have I question and I’ll be pleased if you can reply.
I’ve created a dynamic navigation to a category table
category1
subcategory
subsubcategory
category2
However I don’t know how I can write the routes correctly
module.config
‘home’ => array(
‘type’ => ‘Literal’,
‘options’ => array(
‘route’ => ‘/’,
‘defaults’ => array(
‘controller’ => ‘Application\Controller\Index’,
‘action’ => ‘index’
)
),
‘may_terminate’ => true,
‘child_routes’ => array(
‘catalog’ => array(
‘type’ => “Segment”,
‘options’ => array(
‘route’ => “catalog/[:slug1[/:slug2[/:slug3]]]”,
‘defaults’ => array(
‘controller’ => ‘Application\Controller\CatalogCategory’,
‘action’ => ‘index’,
‘slug1’ => ”,
‘slug2’ => ”,
‘slug3’ => ”,
),
),
‘may_terminate’ => true,
),
),
),
I think another away most clean to do it.
I personally prefer query params rather than too many segment as you may need to check all the slash path when not filled.
I was able to manage to get the breadcrumbs and menu with static navigation. But for dynamic navigation, I followed the steps and navigation is working. But the breadcrumbs not showing.
navigation(‘frontend_main_navigation’)->breadcrumbs()->setMinDepth(0); ?>
returns empty pages
consult the documentation, some config (‘pages’) may be missing http://framework.zend.com/manual/current/en/tutorials/tutorial.navigation.html
Hello Sam! I use zend about month. And i try to do this in ZF3 How I can implement that inZF3
$application = $serviceLocator->get(‘Application’);
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();