Search code examples
zend-frameworkzend-framework2zend-framework-routing

ZF2 main router in console


How can I use the url() controller helper inside the console?

I have to generate some urls using an action of the console, but if I call $this->controller->url()->fromRoute(...);, I can only ask for routes defined in the console router.

In other words, I need the ability to call all the routes of the application's main router.

Thanks


Solution

  • I'm not sure, that it is a good decision, but you can change router manually:

    // ConsoleController.php
    
    // Change router to HTTP    
    $this->getEvent()->setRouter($this->getServiceLocator()->get('HttpRouter'));
    
    // Get any HTTP route
    var_dump($this->url()->fromRoute('your_http_route'));
    
    // Change it back, if you want
    $this->getEvent()->setRouter($this->getServiceLocator()->get('Router'));