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,
$entityManager,
$config,
SimpleRouteStack $router,
$viewHelperMgr,
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);
$pluginManager->setService(
Url::class,
$this->viewHelperManager->get(‘url’)
);
/** 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);
$pluginManager->setService(
ServerUrl::class,
function () {
$urlHelper = new ServerUrl();
$urlHelper->setHost($this->config[‘hostname’]);
return $urlHelper->getHost();
}
);
$this->renderer->setHelperPluginManager($pluginManager);
parent::__construct($name);
}
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?