简体   繁体   English

symfony 5 - 从子目录到资产的路径

[英]symfony 5 - Path to the assets from subdirectories

I've got a problem with the path of assets on Symfony 5. This is a structure of assets in the project:我在 Symfony 5 上的资产路径有问题。这是项目中的资产结构:

app/
    public/
        css/
            style.css 
            ...
            ...

Everythings works fine when I do not have subdirectories in URL, but when URL looks like:当我在 URL 中没有子目录时一切正常,但是当 URL 看起来像:

https://example.com/catalog/course

Symfony looking for file: Symfony 寻找文件:

https://example.com/catalog/course/style/style.css

The properly address is:正确的地址是:

https://example.com/css/style.css

That works like we can read in documentation: https://symfony.com/doc/current/templates.html#linking-to-css-javascript-and-image-assets这就像我们可以在文档中阅读的那样工作: https://symfony.com/doc/current/templates.html#linking-to-css-javascript-and-image-assets

Regardless of the subdirectories in URl, I have only one style.css.不管URl里面的子目录,我只有一个style.css。 How can I determine to looking for the properly path of assets?如何确定寻找资产的正确路径?

In Symfony, you can use the asset() function to generate the correct URL for an asset, regardless of the subdirectories in the URL. This function takes the path to the asset, relative to the public directory, as its argument.在 Symfony 中,您可以使用 asset() function 为资产生成正确的 URL,而不管 URL 中的子目录。这个 function 将相对于公共目录的资产路径作为其参数。 For example, to link to the style.css file in the example you provided, you would use the following code in your Twig template:例如,要链接到您提供的示例中的 style.css 文件,您可以在 Twig 模板中使用以下代码:

<link rel="stylesheet" href="{{ asset('css/style.css') }}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM