[NFR]: Assets which respect the base static URI from `\Phalcon\Mvc\Url`
Hello,
I am just migrating from 4.2 to 5.6. I see there are a lot of fixes, features that I wanted to congrat your hard work.
Is your feature request related to a problem? Please describe.
I was using assets manager efficiently. But now I have to use it something like that. Removing universal prefix support was really necessary? We were already able to define external targets that we could add our own prefixes by calling url service or something like that. That felt me weird and wanted to know if there is a decent reason behind.
# Common Assets
# -------------------------------------------- #
$this->assets
->collection('header')
// ->setPrefix($this->url->getStatic())
->addCss($this->url->getStatic('css/bootstrap.min.css'))
->addCss($this->url->getStatic('css/style.css'))
->addCss($this->url->getStatic('css/module-'. $moduleName .'.css'))
->addJs($this->url->getStatic('js/livequery.js'));
In Phalcon 4.2, the only requirement was defining the assets manager like below:
$this->DI->setShared('assets', ['className' => 'Phalcon\Assets\Manager']);
But now I have to define all the services in the outer scope of the service definition, which is not efficient because they'll create another instance even if I might not use them in the request.
$ESCAPER = new \Phalcon\Html\Escaper();
$TAG_FACTORY = new \Phalcon\Html\TagFactory($ESCAPER);
$ASSETS = new Phalcon\Assets\Manager($TAG_FACTORY);
$this->DI->setShared('escaper', $ESCAPER);
$this->DI->setShared('assets', $ASSETS);
Describe the solution you'd like
- I'd like to use assets manager in 5.6 like 4.2.
- Assets are certainly related to
TagFactoryhowever, it requires strictly an argument which is not efficient if there are no any alternatives. -
TagFactoryalso requires escaper instance which was annoyed that I might not need escaper at all.
I've ended up by doing below way that it works:
$this->DI->setShared('escaper', function ()
{
return new \Phalcon\Html\Escaper();
});
$this->DI->setShared('tag', function ()
{
return new \Phalcon\Html\TagFactory($this->getShared('escaper'));
});
$this->DI->setShared('assets', function ()
{
return new \Phalcon\Assets\Manager($this->getShared('tag'));
});
That is not antipattern or something like that, right?
Mustafa, Thanks.
I just wanted to comment that I am having this issue as well (the one in the title, the post seems to describe something else).
I'm migrating from 3.4 to 5.7, so yeah there's A LOT of changes. One thing I noticed is that in my code I simply called:
$di->get('url')->setBaseUri('/my_app/');
And the assest urls would be prepended with /my_app/ But now in Phalcon 5.4 that doesn't happen, instead they are just something like '/css/style.css' which is a 404.
IMO, I would expect the Assets Manager to use Url get() method when generating the paths for assets.