Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

Symfony assets loading issue caused by my route


Hi,

The problem i have in my Symfony 6 project happens when my routes have more than one "/" like so :
#[Route('/user/{id}', name: 'edit_user')]

This prevents any of my assets from loading correctly. I got a vague understanding of how Metronic loads these in my pages by checking master.html.twig, ThemeHelper.php and services.yaml but i cannot think of a practical way to solve this.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (4)


Hi,

Sorry for the delay. The assets load from this file /starterkit/src/Service/ThemeHelper.php in function function asset. We will fix it as soon as possible and provide you fix as workaround soon.

Thanks



We have fixed the issue with loading assets in your Symfony project.
To fix this, we have used Asset Component.
https://symfony.com/doc/current/components/asset.html

We installed it with Composer by running the following command:

composer require symfony/asset

Then, we imported the Package and EmptyVersionStrategy classes from the component in the /starterkit/src/Service/ThemeHelper.php file:

use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;

Finally, we modified the asset() function in the ThemeHelper.php file to use the Package class to generate the correct URL for the asset files, relative to the public directory:

$filesystem = new Filesystem();
if (!$filesystem->exists("/public/assets/manifest.json")) {
return $this->package->getUrl(sprintf("/assets/%s", $path));
}

We hope this solves your problem. We will include the fix in the next updates. If you have any questions or feedback, please let us know and we’ll try to assist you.

Thanks



Hi,

$package was not recognized so i generated a class instance before calling it. It seems to work just fine, let me know if you are dealing with this error in a different / more optimized way.


$filesystem = new Filesystem();
if (!$filesystem->exists("/public/assets/manifest.json")) {
$this->package = new Package(new EmptyVersionStrategy());
return $this->package->getUrl(sprintf("/assets/%s", $path));
}


Thank you very much.



Hi,

Yes, the class need to be initialized. We put it in the _construct function.


public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
$this->package = new Package(new EmptyVersionStrategy());
}


Thanks


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(