【发布时间】:2021-01-17 07:31:13
【问题描述】:
我是 Symfony 的新手(这是我第一次在这里发帖)。我正在做一个项目,但是 Symfony 不会加载 css 文件。我已经安装了 Asset。
我尝试使用资产{{}} 和不加载文件。我也尝试过包含 /public 和不包含。
我尝试过的方法:
<link rel="stylesheet" href="{{ asset('/build/css/app.css') }}">
<link rel="stylesheet" href="{{ asset('/public/build/css/app.css') }}">
<link rel="stylesheet" href="/public/build/css/app.css'">
<link rel="stylesheet" href="/build/css/app.css'">
<link rel="stylesheet" href="../../public/build/css/app.css'">
任何帮助将不胜感激,谢谢!
我有同样的问题,但只使用 apache 虚拟主机:虚拟主机配置:
<VirtualHost *:80>
ServerName progdemo.local
ServerAlias www.progdemo.local
DocumentRoot "C:/wamp64/www/les4/public"
<Directory "C:/wamp64/www/les4/public">
AllowOverride None
Order Allow,Deny
Allow from All
Require local
</Directory>
ErrorLog C:/wamp64/www/les4/var/log/project_error.log
CustomLog C:/wamp64/www/les4/var/log/project_access.log combined
# from .htacces :
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>
# from .htacces End
</VirtualHost>
在我的 WAMPserver 运行的情况下,直接访问 http://localhost/les4/public/,可以完美运行。 转到 progdemo.local,不渲染 css 样式表。
模板:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Let's Explore Symfony 4</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css"
integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy"
crossorigin="anonymous">
<link href="/les4/public/css/miostile.css" rel="stylesheet">
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">Home</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/hello">Hello Page</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<main role="main" class="container main">
<div>
<h1>Let's Explore Symfony 4</h1>
<p class="lead">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras rutrum sapien mauris, venenatis
facilisis neque tincidunt vel. Maecenas vel felis vel turpis scelerisque eleifend. Fusce nec purus egestas,
efficitur nisi ac, ultrices nulla. Pellentesque eu mollis tortor, in mollis nisl. Maecenas rhoncus quam non
lacinia mollis.</p>
</div>
</main>
</body>
</html>
“miostile.css”文件:
.main {margin-top: 90px; background-color: aqua; }
Chrome 控制台消息:
miostile.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
miostile.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)"
一些提示:
- 页面源代码完全相同
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Let's Explore Symfony 4</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css"
integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy"
crossorigin="anonymous">
<link href="/les4/public/css/miostile.css" rel="stylesheet">
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">Home</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/hello">Hello Page</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<main role="main" class="container main">
<div>
<h1>Let's Explore Symfony 4</h1>
<p class="lead">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras rutrum sapien mauris, venenatis
facilisis neque tincidunt vel. Maecenas vel felis vel turpis scelerisque eleifend. Fusce nec purus egestas,
efficitur nisi ac, ultrices nulla. Pellentesque eu mollis tortor, in mollis nisl. Maecenas rhoncus quam non
lacinia mollis.</p>
</div>
</main>
.... and so on
</html>
但是...!!:如果,在OK页面的浏览源代码显示中,我点击了
<link href="/les4/public/css/miostile.css" rel="stylesheet">
打开一个新页面“http://localhost/les4/public/css/miostile.css”显示css文件内容
".main {margin-top: 90px; background-color: aqua; } "
在 KO 页面上做同样的事情,打开的页面 "http://progdemo.local/les4/public/css/miostile.css" 显示 symfony 错误 "No route found for "GET /les4/public/css /miostile.css"
"ERROR
11:10:27
request Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /les4/public/css/miostile.css"" at C:\wamp64\www\les4\vendor\symfony\http-kernel\EventListener\RouterListener.php line 136"
- 如果使用“资产”功能也会出现同样的问题
<link href={{ asset ('css/miostile.css') }} rel="stylesheet">
OK 页面显示
<link href=/les4/public/css/miostile.css rel="stylesheet">
并且仍然可以完美运行。 KO页面显示
<link href=/css/miostile.css rel="stylesheet">
但效果不佳(css 未呈现并在单击时显示“未找到“GET /css/miostile.css”的路由。
如果需要进一步调查可用
Ciao!
【问题讨论】:
-
您的浏览器的网络控制台会告诉您什么?
-
另外,您尝试过什么来解决这个问题?据我了解您的文件列表,CSS 存储在
public/build/app.css,而不是任何子文件夹中 -
我尝试使用以下两种方式加载 css 文件: 和 以及没有 /public。该文件位于子 css 文件中。控制台显示 GET localhost:8000/css/app.css net::ERR_ABORTED 404 (Not Found)
-
这仍然是错误的,除非您将 CSS 文件放在
public/build/css/app.css- 根据您文件的屏幕截图,这只是 不是 您使用的位置远 -
是的,我把它移到了css文件中,但是控制台说:加载资源失败:服务器响应状态为404(未找到)
标签: php css symfony bootstrap-4