I am running some console command to generate static page using layout file where layout is using basePath is used as following way
<?= $this->inlineScript()
->prependFile($this->basePath('/dist/js/vendor.js'),'text/javascript',['defer'=>true])
->prependFile($this->basePath('/dist/js/manifest.js'),'text/javascript',['defer'=>true])
->prependFile($this->basePath('/dist/js/app.js'),'text/javascript',['defer'=>true])
?>
The given above code is working perfectly fine while request served using web browser. But when I try to run console command this will throw me an error.
The application has thrown an exception!
Laminas\View\Exception\RuntimeException
No base path provided
I am using following code at Module.php file
public function getServiceConfig()
{
return [
'initializers' => [
function ($instance, $services) {
if (! Console::isConsole()) {
return;
}
if (! $instance instanceof HelperPluginManager) {
return;
}
$instance->setFactory('basePath', function ($sm) use ($services) {
$config = $services->get('Config');
$config = $config['view_manager'];
$basePathHelper = new ViewHelper\BasePath();
$basePath = '/';
if (isset($config['base_path'])) {
$basePath = $config['base_path'];
}
$basePathHelper->setBasePath($basePath);
return $basePathHelper;
});
},
]];
}