ZF2 main router in console

60 views Asked by At

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

1

There are 1 answers

0
Rodin Vasiliy On BEST ANSWER

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'));