Reputation: 685
I wrote a module to generate sitemap xml files. I want to generate them in a console route via cronjob, but the problem is that I get an exception Route with name "xy" not found
when I iterate through the navigation container. The route definitely exists. If I call it in a web request, everything works fine. The stack trace looks like the following:
======================================================================
The application has thrown an exception!
======================================================================
Zend\Mvc\Router\Exception\RuntimeException
Route with name "home" not found
----------------------------------------------------------------------
vendor/zendframework/zend-mvc/src/Router/SimpleRouteStack.php:328
#0 vendor/zendframework/zend-navigation/src/Page/Mvc.php(260): Zend\Mvc\Router\SimpleRouteStack->assemble(Array, Array)
#1 vendor/zendframework/zend-navigation/src/Page/AbstractPage.php(999): Zend\Navigation\Page\Mvc->getHref()
#2 vendor/zendframework/zend-navigation/src/Page/AbstractPage.php(1035): Zend\Navigation\Page\AbstractPage->get('href')
#3 module/Sitemap/src/Sitemap/Service/GeneratorService.php(83): Zend\Navigation\Page\AbstractPage->__get('href')
#4 module/Sitemap/src/Sitemap/Controller/Cli/GeneratorController.php(31): Sitemap\Service\GeneratorService->generateSitemaps()
#5 vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(82): Sitemap\Controller\Cli\GeneratorController->indexAction()
#6 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#7 vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#8 vendor/zendframework/zend-eventmanager/src/EventManager.php(214): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(118): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 vendor/zendframework/zend-mvc/src/DispatchListener.php(93): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Console\Request), Object(Zend\Console\Response))
#11 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#12 vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#13 vendor/zendframework/zend-eventmanager/src/EventManager.php(214): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 vendor/zendframework/zend-mvc/src/Application.php(314): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#15 public/index.php(15): Zend\Mvc\Application->run()
#16 {main}
======================================================================
Upvotes: 0
Views: 450
Reputation: 1382
You're using the CLI to process or generate your sitemap XML and thus using another Router, the CLI one.
So you need to fetch the other Router class -> HTTP Router.
array(
// HttpRouter
'router' => array(
'routes' => array(
// HTTP routes are here
)
),
// Console Router
'console' => array(
'router' => array(
'routes' => array(
// Console routes go here
)
)
),
)
So in order to get the HttpRouter:
$serviceManager->get('HttpRouter')
So if you want to use the HttpRouter you can do the following within your CLI Controller(s):
$cliRouter = $this->getEvent()->getRouter();
$this->getEvent()->setRouter($httpRouter);
// Http Routers can be called with the plugins
$this->url()->fromRoute('home');
// Use CLI router again
$this->getEvent()->setRouter($cliRouter);
Calling $this->url()->fromRoute('home');
this will throw your error in your question when using the CLI router. The "home" is not defined within your console routing and thus can not be found. Change your router to Http as you defined it in your HTTP routing.
Upvotes: 3