为什么在blade模板中获取不到CSS资源的完整路径?
Why I can't obtain the complete path of a CSS resource in a blade template?
我是 Laravel 的新手。我正在开发 Laravel 5.4 应用程序,使用 blade 检索 CSS 资源的完整 URL 时遇到以下问题。
这是我的 app.blade.php 文件,代表我所有观点的模板:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!--<link href="/css/app.css" rel="stylesheet">-->
<!--<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet" type="text/css" >-->
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="/js/app.js"></script>
</head>
<body>
<div class="container" style="width: 60%; margin-bottom: 50px;">
@yield('content')
</div>
@yield('footer')
</body>
</html>
如您所见,遵循此 SO post:
我是这样定义这个 CSS 资源的:
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >
从正确的位置检索 http://localhost/HotelRegistration/public/css/app.css 资源。
问题在于,以这种方式打开一个 URL,例如 http://localhost/HotelRegistration/public/registration
我还是在生成的页面中获取了错误的CSS link,这个:
<link href="/css/app.css" rel="stylesheet">
这样做我希望得到正确的 http://localhost/HotelRegistration/public/css/app.css
我也尝试使用此替代方法导入它 link:
<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet" type="text/css" >
但我得到了相同的结果。
怎么了?我错过了什么?我怎样才能解决这个问题并获得 CSS 的正确 url 包括整个路径?
在 apache 配置文件中使用虚拟主机你的根目录是 public 目录而不是你的本地主机根目录(当然替换 YOUR_LOCALHOST_ROOT_PATH):
<VirtualHost *:80>
ServerAdmin test@test.com
DocumentRoot YOUR_LOCALHOST_ROOT_PATH/HotelRegistration/public
ServerName hotel.dev
<Directory "YOUR_LOCALHOST_ROOT_PATH/HotelRegistration/public">
AllowOverride All
Require all granted
</Directory>
当然 hotel.dev 在 Internet 上不存在,没问题,使用您的系统主机文件!
在你的主机文件中(在 windows : C:/Windows/System32/drivers/etc/host , linux /etc/host )
添加这一行:
127.0.0.1 hotel.dev
您也可以添加更多行:
127.0.0.1 hotel.dev another.dev youtestsite.dev
重新启动浏览器和 apache,它应该会立即运行
在那之后你应该没有任何路径问题!
如果您在本地主机上工作,您应该从您的目录根项目 运行命令行(例如:HotelRegistration /) :
php artisan serve
默认情况下,您可以使用 url 访问您的项目:http://127.0.0.1:8000/ and you css link will look like http://127.0.0.1:8000/css/app.css
如果你想托管在你的远程服务器上,你可以使用.htaccess重写模式重定向到index.php的public目录.
首先,使用 artisan serve 是个好主意:
php artisan serve
这会减少很多麻烦。如果您在 Mac,请尝试 valet
。真的很好很轻。
其次,您需要按以下方式导入 css:
<link href="{!! asset('css/app.css') !!}" rel="stylesheet" type="text/css" >
希望这能解决您的问题。
我是 Laravel 的新手。我正在开发 Laravel 5.4 应用程序,使用 blade 检索 CSS 资源的完整 URL 时遇到以下问题。
这是我的 app.blade.php 文件,代表我所有观点的模板:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!--<link href="/css/app.css" rel="stylesheet">-->
<!--<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet" type="text/css" >-->
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="/js/app.js"></script>
</head>
<body>
<div class="container" style="width: 60%; margin-bottom: 50px;">
@yield('content')
</div>
@yield('footer')
</body>
</html>
如您所见,遵循此 SO post:
我是这样定义这个 CSS 资源的:
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >
从正确的位置检索 http://localhost/HotelRegistration/public/css/app.css 资源。
问题在于,以这种方式打开一个 URL,例如 http://localhost/HotelRegistration/public/registration
我还是在生成的页面中获取了错误的CSS link,这个:
<link href="/css/app.css" rel="stylesheet">
这样做我希望得到正确的 http://localhost/HotelRegistration/public/css/app.css
我也尝试使用此替代方法导入它 link:
<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet" type="text/css" >
但我得到了相同的结果。
怎么了?我错过了什么?我怎样才能解决这个问题并获得 CSS 的正确 url 包括整个路径?
在 apache 配置文件中使用虚拟主机你的根目录是 public 目录而不是你的本地主机根目录(当然替换 YOUR_LOCALHOST_ROOT_PATH):
<VirtualHost *:80>
ServerAdmin test@test.com
DocumentRoot YOUR_LOCALHOST_ROOT_PATH/HotelRegistration/public
ServerName hotel.dev
<Directory "YOUR_LOCALHOST_ROOT_PATH/HotelRegistration/public">
AllowOverride All
Require all granted
</Directory>
当然 hotel.dev 在 Internet 上不存在,没问题,使用您的系统主机文件! 在你的主机文件中(在 windows : C:/Windows/System32/drivers/etc/host , linux /etc/host ) 添加这一行:
127.0.0.1 hotel.dev
您也可以添加更多行:
127.0.0.1 hotel.dev another.dev youtestsite.dev
重新启动浏览器和 apache,它应该会立即运行
在那之后你应该没有任何路径问题!
如果您在本地主机上工作,您应该从您的目录根项目 运行命令行(例如:HotelRegistration /) :
php artisan serve
默认情况下,您可以使用 url 访问您的项目:http://127.0.0.1:8000/ and you css link will look like http://127.0.0.1:8000/css/app.css
如果你想托管在你的远程服务器上,你可以使用.htaccess重写模式重定向到index.php的public目录.
首先,使用 artisan serve 是个好主意:
php artisan serve
这会减少很多麻烦。如果您在 Mac,请尝试 valet
。真的很好很轻。
其次,您需要按以下方式导入 css:
<link href="{!! asset('css/app.css') !!}" rel="stylesheet" type="text/css" >
希望这能解决您的问题。