Laminas CLI + URL View Helper

I have a MVC application and I’m building a CLI command to generate HTML email and send to my users in a scheduled batch job (that will invoke the command). I’m using Symfony Command component per the fantastic guide by Abdul Malik guide.

I have written a factory to inject the ViewHelperManager and the ViewRenderer into to the CLI command class.

public function __construct(
    PhpRenderer $renderer,
    SimpleRouteStack $router,
    string $name = null
) {
    $this->renderer          = $renderer;
    $this->entityManager     = $entityManager;
    $this->config            = $config;
    $this->router            = $router;
    $this->viewHelperManager = $viewHelperMgr;

    // Setup plugin Manager
    $pluginManager = $this->renderer->getHelperPluginManager();
    $pluginManager->setAlias(‘url’, Url::class);
    /** This has been commented out as this was my previous attempt but the url helper is returning blank.
     * $pluginManager->setService(Url::class, function($name = null, $params = [], $options = [], $reuseMatchedParams = false) {
     * $url = new Url($name, $params, $options, $reuseMatchedParams);
     * $url->setRouter($this->router);
     * });
    $pluginManager->setAlias(‘serverUrl’, ServerUrl::class);
        function () {
            $urlHelper = new ServerUrl();

            return $urlHelper->getHost();



One challenge I have is the URL view helper that is called in the HTML views rendered by the ViewRenderer. I was expecting it to be able to generate the FQDN URLs based on the simple route stack that has been written in the MVC module configuration. I also tried injecting the router in the CLI command class, passing it into the URL as a service but it still didn’t work. Has anyone been successful in generating the FQDN URLs in the CLI?

Have you tried laminas-cli? The component can use the existing configuration of your laminas-mvc application.

I will certainly check it out. Thanks!

Hi froschdesign, I just went through my code again and confirmed that its using laminas-cli. Here’s a quick peak at the execute function:

protected function execute(InputInterface $input, OutputInterface $output)
    $output->writeln('Host is: ' . $this->renderer->serverUrl());
    $output->writeln('URL is: ' . $this->renderer->url('test',['action' => 'view','id' => 1])); 

If the MVC routes have been inherited by CLI, the 2nd line of output should show the complete URL for the route, action and ID that I passed. However, as you can see in the screen shot below, the output is still not what I expected. Am I missing something? My only other alternative is to design the HTML email so that it doesn’t contain any specific URL based on the routes, which isn’t really where I would like to go. Thoughts?