Generate XML Sitemap in Zend Framework 2
I personally use Zend Framework 2 and when I saw the requirement of sitemap I searched out many platforms but unable to create a sitemap. Then I use some online sitemap generator which generate sitemap by crawling your url. I use this sitemap for some of the time then finally I figured out the way to create dynamic sitemap using zf2 Navigation. Here I am writing how to Generate Xml Sitemap Zend Framework 2 step by step with code.
Zf2 starts with default Application Module. This is the main module every other module use the application module layout(we can still disable to not use the application module layout). Firstly, we need to tell our application which NavigationFactory to use when using the bundled navigation view helpers. To tell ZF2 to use this default factory, I simply add a navigation key to the service manager in the Application’s module.config.php. I am posting screenshot of my code. It also have some other declaration inside service_manage, you just have to use factories array.
To add navigation according to each module I simply create this container inside Application’s module.config.php . You can add this navigation array in the last of the main array . Don’t enter randomly route and label in default array, it verifies whether it exist or not otherwise it will give internal server route.
Now I work on view to show the sitemap. First I created a route for the sitemap. You can add the route in any module’s config file but I prefer to write it in the Application’s module.config.php file.
Since my action name is sitemap so I create one action in Application controller. I have added $viewmodel->setTerminal(true) to disable the layout in view.
Create one sitemap.phtml file inside views of Application module(if you have chosen application module). Add the following content in the view file.
Just visit yourdomain.com/sitemap and hurray your sitemap is created. Feel free to comment in the comment box below if anyone face any error during implementation. I love to help you folks.
Note : To know Why Oracle Patent lawsuit’s against Google. Read Here