[英]Symfony 4 route to assets folder
I am trying to load the stylesheet in my Assets folder following the instruction giving on this page: 我试图按照此页面上的说明将样式表加载到我的Assets文件夹中:
https://symfony.com/doc/current/best_practices/web-assets.html https://symfony.com/doc/current/best_practices/web-assets.html
But I fail to navigate from the base.html.twig to the folder using: 但是我无法使用以下方法从base.html.twig导航到该文件夹:
<link rel="stylesheet" type="text/css" href="/assets/css/style.css" />
my file structure looks like this: 我的文件结构如下所示:
templates
base.html.twig
assets
css
style.css
Any ideas why this doesn't work? 任何想法为什么这不起作用?
您可以尝试使用symfony的资产函数:
<link rel="stylesheet" href="{{ asset('css/style.css') }}" />
In your AppExtension: 在您的AppExtension中:
// register function here
public function getFunctions() {
return [
new TwigFunction('load_js_asset', array($this, 'fileGetJsAsset')),
];
}
// register filter here
public function getFilters() : array
{
return [
new TwigFilter('html_entity_decode',
[$this, 'html_entity_decode'],
['is_safe' => ['html']]),
}
/**
* @param string $string
* @return string
*/
public function html_entity_decode(string $string) : string {
return html_entity_decode($string);
}
/**
* @param string $file
* @param bool $embedScriptTag
* @return string
*/
public function fileGetJsAsset(string $file, bool $embedScriptTag = true) : string {
/** @var bool|string $content */
$content = file_get_contents(__DIR__ . '/../../assets/js/' . $file);
if ($content === false) {
throw new FileException('Could not found or read file: ' . $file);
}
if (!$embedScriptTag) {
return $content;
}
return '<script>' . $content . '</script>';
}
Now in your view load js assets like this: 现在在您的视图中加载js资产,如下所示:
// loading from root assets/js folder //从根资产/ js文件夹加载
{{ load_js_asset('core/jquery.min.js', true)|html_entity_decode }} {{ load_js_asset('core/popper.min.js')|html_entity_decode }} {{ load_js_asset('my-beauty-js-file.js')|html_entity_decode }}`
I hope it helps. 希望对您有所帮助。
Maybe you will need also htmlspecialchars_decode filter. 也许您还需要htmlspecialchars_decode过滤器。
http://symfony.com/doc/3.4/best_practices/web-assets.html http://symfony.com/doc/3.4/best_practices/web-assets.html
Assets function still being usefull in Symfony 4. 资产功能在Symfony 4中仍然有用。
A couple of possible solutions - without more detail I can't be sure if they will definitely solve the problem or not: 几个可能的解决方案-如果没有更多细节,我不确定它们是否一定能解决问题:
First, did you configure webpack-encore
and run the build? 首先,您是否配置了
webpack-encore
并运行了构建? Without that the resources won't be there and nginx
will generate a 404 trying to route to it. 否则,资源将不存在,
nginx
将生成404尝试路由到该资源。
Second, if you're running with Docker, you may not have mounted the ./app/public/
folder onto the nginx
container so that the files could be served. 其次,如果您正在使用Docker运行,则可能未将
./app/public/
文件夹安装到nginx
容器上,以便可以提供文件。 If you are getting a Symfony 404 when you go to those files, this may be the issue. 如果转到这些文件时获得Symfony 404,则可能是问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.