在 Laravel 中将公共添加到资产路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32810231/
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
Add Public to asset path in Laravel
提问by user3407278
I want to install laravel in shared hosting and I followed the steps here https://stackoverflow.com/a/28449523but my asset path doesn't include the public directory
我想在共享主机中安装 laravel,我按照这里的步骤https://stackoverflow.com/a/28449523但我的资产路径不包括公共目录
Instead of this
而不是这个
<link href='http://example.com/public/assets/css/style.css' type='text/css' media='all' />
I'm getting this
我得到这个
<link href='http://example.com/assets/css/style.css' type='text/css' media='all' />
How do I change the directory of the asset folder(add public to assets) without changing any core classes?
如何在不更改任何核心类的情况下更改资产文件夹的目录(将公共添加到资产)?
采纳答案by Robo Robok
Before Laravel 5.7.14
Laravel 5.7.14 之前
Take a look at Illuminate\Routing\UrlGenerator
class and its asset($path, $secure = null)
method. This class is being put to container by url
key. What you can do is:
看看Illuminate\Routing\UrlGenerator
类及其asset($path, $secure = null)
方法。此类正在通过url
密钥放入容器。你可以做的是:
- Add your own class, extending
UrlGenerator
; - Add
asset($path, $secure = null)
method to your own class, making it return whatever you need; - Create a service provider and register it in
config/app.php
; - In your service provider's
register()
method, bind your new class to the container byurl
key.
- 添加您自己的类,扩展
UrlGenerator
; - 将
asset($path, $secure = null)
方法添加到您自己的类中,使其返回您需要的任何内容; - 创建一个服务提供者并在其中注册
config/app.php
; - 在您的服务提供者的
register()
方法中,通过url
键将您的新类绑定到容器。
This way, you don't touch core files at all and your Laravel application is still update friendly.
这样,您根本不会接触核心文件,并且您的 Laravel 应用程序仍然易于更新。
Update for Laravel 5.7.14 and later
Laravel 5.7.14 及更高版本的更新
As other answers state, there is the ASSET_URL
.env option, which makes it much easier to change the public path. Laravel introduced it in Laravel 5.7.14, which was released about 3 years after my original answer.
正如其他答案所述,有ASSET_URL
.env 选项,可以更轻松地更改公共路径。Laravel 在 Laravel 5.7.14 中引入了它,该版本在我最初的回答大约 3 年后发布。
回答by Abid_015
Add ASSET_URL=publicin your .envfile and run php artisan config:cache
在.env文件中添加ASSET_URL=public并运行php artisan config:cache
回答by Udo E.
For the latest version of Laravel - Laravel 5.8, there is a key in config/app.phpwith name asset_url. The value of this key determines the value that the asset()method returns. Normally, you should set the value of this key to the base url of your website if all your asset folders are in the default location (this default location is the publicfolder under the root directory of your Laravel installation).
对于最新版本的 Laravel - Laravel 5.8,在config/app.php 中有一个名为asset_url 的键。这个键的值决定了asset()方法返回的值。通常,如果您的所有资产文件夹都在默认位置(此默认位置是Laravel 安装根目录下的公共文件夹),您应该将此键的值设置为您网站的基本 url 。
For example if your website url is "https://www.example.com" and you want to access the asset path public/assets/css/sample.cssunder the root folder, set the asset_url in config/app.php like this:
例如,如果您的网站 url 是“ https://www.example.com”,并且您想访问根文件夹下的资产路径public/assets/css/sample.css,请在 config/app.php 中设置 asset_url,如这个:
'asset_url' => env('ASSET_URL', 'https://www.example.com'),
and use the asset function like this:
并像这样使用资产功能:
asset('assets/css/sample.css')
Thereafter, reconfigure your cache by running this within your laravel installation folder:
此后,通过在 Laravel 安装文件夹中运行它来重新配置缓存:
php artisan config:cache
This will update the bootstrap/cache/config.phpfile. If you check your browser, the generated url for your asset would be "https://www.example.com/assets/css/sample.css".
这将更新bootstrap/cache/config.php文件。如果您检查浏览器,则为您的资产生成的 url 将是“ https://www.example.com/assets/css/sample.css”。
One way you can have a valid url like: "https://www.example.com/public/assets/css/sample.css" is by creating another folder named publicinside your already existing public folder - which is non-intuitive for me. However, if you do this, then you have to include this path when using the asset function:
你可以有这样一个有效的URL的一种方式:“ https://www.example.com/public/assets/css/sample.css”是创建一个名为其他文件夹公共您已经现有的公共文件夹内-这是不直观为了我。但是,如果这样做,则在使用资产功能时必须包含此路径:
asset('public/assets/css/sample.css')
回答by Dibyendu Mitra Roy
A relatively easier hack is to add the folder in the return value of the asset() function inside \vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php
一个相对简单的hack是在\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php中的asset()函数的返回值中添加文件夹
public function asset($path, $secure = null)
{
if ($this->isValidUrl($path)) {
return $path;
}
// Once we get the root URL, we will check to see if it contains an index.php
// file in the paths. If it does, we will remove it since it is not needed
// for asset paths, but only for routes to endpoints in the application.
$root = $this->assetRoot
? $this->assetRoot
: $this->formatRoot($this->formatScheme($secure));
// Following 2 lines were added
if($_SERVER['REMOTE_ADDR'] != '127.0.0.1')
$root .= '/public';
return $this->removeIndex($root).'/'.trim($path, '/');
}
Note here that I have added public to the return value based on a condition. So that can be done as well.
请注意,我已根据条件将 public 添加到返回值。所以这也可以做到。