We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to generate URLs in CLI app?

How can I generate URLs in Phalcon CLI?

I have a multimodule application with standard router (Phalcon\Mvc\Router) and CLI worker that sends e-mails.

I want the URLs generated in CLI to be based on the routes from MVC app.

I have a Phalcon\Mvc\View in CLI that works as stand-alone component. It generates the body of e-mail messages. The URLs are generated this way:


$url = $this->url->get([
  'for' => 'panel', //route name
  'controller' => 'verification',
  'action' => 'email',
  'params' => $parameters->getParam('key'),
]);

I have declared the MVC router in CLI app and I get this error:

Unexpected value type: expected object of type Phalcon\CLI\Router, object of type Phalcon\Mvc\Router given.

So, how I can generate URLs in CLI app, URLs that are already defined in MVC router?



15.1k
Accepted
answer

CLI uses a different router module from the normal web apps - Phalcon\ CLI \Router rather than Phalcon\ Mvc \Router.

Quick hack would be to create a second, empty, DI instance, which contains the full Mvc router, and using $this->url->setDI($secondDi);



26.3k

Thanks for your answer! Very interesting!

At the moment my work-around is that I have extended Phalcon's URL component a little bit:


class Url extends \Phalcon\Mvc\Url {

    public function setRouter(\Phalcon\Mvc\RouterInterface $router) {
        $this->_router = $router;
        return $this;
    }

In my CLI bootstrap file:


$di->setShared('url', function () use ($config) {
  $url = new \Baramke\Worker\Library\Url();
  $url->setRouter(require __DIR__.'/../../common/config/routes.php'); //this is my router object from my MVC app
  $url->setBaseUri($config->application->baseUri);
  return $url;
});

And it works for now.

Thanks in advance for any new ideas!

I think it would be good if MVC router could be used in CLI apps (e.g. my situation to produce URLs). Am I right? Or mayby I am wrong, is it a good solution that my queue worker is set up as a CLI app?



15.1k

The CLI uses a different router mainly because it needs to route to different classes. You know, tasks and not controllers, so it does make sense to keep them separate. I had the same problem as you a few weeks ago, and ended up creating a separate bootstrap file for cli.

As for this problem, your solution seems even cleaner than mine, so well done!



26.3k

@nazwa

My solution didn't work when i want to use View component more than one time in a single request. I don't know why and have no time to analyze. Your solution works!

Thanks a lot!



15.1k
edited Nov '14

Ha, that's interesting. I wonder why it desn't work with the view component. Definitely let me know if you ever find out what the problem was.

Too bad I haven't seen this thread when the same issue happened to me a few weeks ago : https://forum.phalcon.io/discussion/15507 Looks like the same workarounds have been found.

is it a good solution that my queue worker is set up as a CLI app?

I think it is. I would say there is no need for both router to share the same router service name. Maybe I'll make a PR on github if that sounds right ?