php Laravel 5 更改 public_path()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31758901/
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 5 change public_path()
提问by user1995781
I am trying to move the public
folder to other place. However, I can't find the place to modify public_path()
variable. Now, the 'public_path()' return the wrong path of folder.
我正在尝试将public
文件夹移动到其他地方。但是,我找不到修改public_path()
变量的地方。现在,'public_path()' 返回错误的文件夹路径。
Where can I set the variable for public_path()
?
我可以在哪里设置变量public_path()
?
回答by Jigs Virani
You can override the public path using ioc container :
您可以使用 ioc 容器覆盖公共路径:
What worked for me flawlessly was adding to public/index.php
the following three lines:
对我来说完美无缺的是添加public/index.php
以下三行:
$app->bind('path.public', function() {
return __DIR__;
});
回答by ruuter
While accepted answer works for requests coming from HTTP, it will not work for artisan
.
虽然接受的答案适用于来自 HTTP 的请求,但它不适用于artisan
.
If you need artisan
to know your custom public path, you need to extend Laravel main Application class. I know it sounds scary, but it's actually very simple.
如果您需要artisan
知道您的自定义公共路径,则需要扩展 Laravel 主 Application 类。我知道这听起来很可怕,但实际上很简单。
All you need to do is following:
Step 1:In the file: bootstrap/app.php
change the very first declaration of $app
variable
您需要做的就是:
第 1 步:在文件中:bootstrap/app.php
更改$app
变量的第一个声明
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
to reflect your own custom Application class:
反映您自己的自定义 Application 类:
$app = new App\Application(
realpath(__DIR__.'/../')
);
Step 2:Define your custom Application class somewhere. For example in app/Application.php
第 2 步:在某处定义您的自定义 Application 类。例如在app/Application.php
<?php namespace App;
class Application extends \Illuminate\Foundation\Application
{
}
Congrats! You have extended Laravel core Application class.
恭喜!您已经扩展了 Laravel 核心 Application 类。
Step 3:Overwrite the publicPath
method. Copy and paste Laravel original method to your new classand change it to your needs. In my particular case I did like this:
第 3 步:覆盖publicPath
方法。将 Laravel 的原始方法复制并粘贴到您的新类中,并根据您的需要进行更改。在我的特殊情况下,我是这样的:
public function publicPath()
{
return $this->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'public_html';
}
That's it! You can overwrite any method in Application class the same way.
就是这样!您可以以相同的方式覆盖 Application 类中的任何方法。
回答by Broshi
I suggest you add it in app/Providers/AppServiceProvider.php
:
我建议你添加它app/Providers/AppServiceProvider.php
:
public function register()
{
$this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
}
This affects artisan
as well.
这也有影响artisan
。
回答by AnasSafi
In laravel 5.6 this work for me ... adding this code to bootstrap/app.php :
在 laravel 5.6 中,这对我有用……将此代码添加到 bootstrap/app.php :
$app->bind('path.public', function() {
return realpath(__DIR__.'/../');
});
where __DIR__.'/../'is path to your public folder
其中__DIR__.'/../'是您的公用文件夹的路径
回答by macieks
$app->bind('path.public', function() {
return base_path().'/mynewpublic';
});
回答by Unai Susperregi
In my case in Laravel 6.0 work this.
This file: bootstrap/app.php
就我而言,在 Laravel 6.0 中使用此方法。
这个文件:bootstrap/app.php
......
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__) );
$app->bind('path.public', function() {
return realpath(__DIR__.'/../httpdocs'); });
.....
I changed the public_path()folder, this example is with httpdocsfolder, you can put watherver you want.
我改变了public_path()文件夹,这个例子是用httpdocs文件夹,你可以随意放。