Laravel 资产网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24794601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel assets url
提问by vikingcode
I have installed Laravel and began trying to write an app. I made some directories for my assets in the same directory as /app. But when I try to visit an image in my localhost for example: http://localhost/assets/images/image.png
我已经安装了 Laravel 并开始尝试编写一个应用程序。我在与 /app 相同的目录中为我的资产创建了一些目录。但是,例如,当我尝试访问本地主机中的图像时: http://localhost/assets/images/image.png
I've also tried it on: http://localhost/app/assets/images/image.png
我也试过: http://localhost/app/assets/images/image.png
I ran into the problem when I was trying to set a background image in a view, and it kept going to 404.
当我尝试在视图中设置背景图像时遇到了这个问题,它一直转到 404。
This is what my app looks like under app/
这是我的应用程序在 app/ 下的样子
->app
->assets
->commands
->config
->controllers
->database
->lang
->models
->start
->storage
->tests
->views
app.txt
routes.php
filters.php
But I get errors about requests. I have double checked that the url is correct.
但是我收到有关请求的错误。我已经仔细检查了 url 是否正确。
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
回答by Walid Ammar
You have to put all your assets in app/public
folder, and to access them from your views you can use asset()
helper method.
您必须将所有资产放在app/public
文件夹中,并从您的视图中访问它们,您可以使用asset()
辅助方法。
Ex. you can retrieve assets/images/image.png
in your view as following:
前任。您可以assets/images/image.png
在您的视图中检索如下:
<img src="{{asset('assets/images/image.png')}}">
<img src="{{asset('assets/images/image.png')}}">
回答by Javier Albadan
Besides put all your assets in the publicfolder, you can use the HTML::image()
Method, and only needs an argument which is the path to the image, relative on the public folder, as well:
除了将所有资产放在公共文件夹中之外,您还可以使用该HTML::image()
方法,并且只需要一个参数,即图像的路径,相对于公共文件夹,以及:
{{ HTML::image('imgs/picture.jpg') }}
Which generates the follow HTML code:
它生成以下 HTML 代码:
<img src="http://localhost:8000/imgs/picture.jpg">
The link to other elements of HTML::image()
Method: http://laravel-recipes.com/recipes/185/generating-an-html-image-element
HTML::image()
方法其他元素的链接:http: //laravel-recipes.com/recipes/185/generating-an-html-image-element
回答by Navendu Kumar
You have to do two steps:
你必须做两个步骤:
- Put all your files (css,js,html code, etc.) into the public folder.
- Use
url({{ URL::asset('images/slides/2.jpg') }})
whereimages/slides/2.jpg
is path of your content.
- 将所有文件(css、js、html 代码等)放入公共文件夹。
- 使用
url({{ URL::asset('images/slides/2.jpg') }})
whereimages/slides/2.jpg
is 内容路径。
Similarly you can call js, css etc.
同样,您可以调用 js、css 等。