php 更改 Laravel 5 中的存储路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31841392/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:39:18  来源:igfitidea点击:

Change the storage path in Laravel 5

phplaravellaravel-5.1

提问by spacek33z

I want to change the storage path Laravel 5.1 uses to something like /home/test/storage. This has the advantage that these files are not stored in the repository, which is fairly ugly I think. In Laravel 4 this was very simple with bootstrap/paths.php.

我想将 Laravel 5.1 使用的存储路径更改为/home/test/storage. 这样做的好处是这些文件不存储在存储库中,我认为这相当难看。在 Laravel 4 中,使用bootstrap/paths.php.

In Laravel 5 it this works by using $app->useStoragePath('/path/')in bootstrap/app.php. However, I want to define the storage path with a config option, like $app->useStoragePath(config('app.storage_path'). The config option calls an environment variable or returns a default location.

在 Laravel 5 中,它通过使用$app->useStoragePath('/path/')in 来工作bootstrap/app.php。但是,我想使用配置选项定义存储路径,例如$app->useStoragePath(config('app.storage_path'). config 选项调用环境变量或返回默认位置。

Doing this results in a Uncaught exception 'ReflectionException' with message 'Class config does not exist'; this makes sense, because this function is not loaded yet.

这样做会导致Uncaught exception 'ReflectionException' with message 'Class config does not exist'; 这是有道理的,因为这个函数还没有加载。

I tried setting the storage path just after booting:

我尝试在启动后设置存储路径:

$app->booted(function () use ($app) {
    $app->useStoragePath(config('app.storage_root'));
});

This changed nothing. I also tried directly binding it to path.storage:

这没有任何改变。我也尝试将它直接绑定到path.storage

$app->bind('path.storage', function ($app) {
    return config('app.storage_root');
});

The last option works partially; the view cache is now placed in the correct location, but the logs are still at the old location.

最后一个选项部分起作用;视图缓存现在放置在正确的位置,但日志仍在旧位置。

回答by Oli Soproni B.

Here is a simple solution of changing the storage path in Laravel 5like we do in Laravel 4

这是在Laravel 5中更改存储路径的简单解决方案,就像我们在Laravel 4 中所做的那样

on bootstrap/app.php

在引导程序/app.php

# new storage path
# base_path() -> returns root path
$path_storage = base_path() . "../../storage";

# override already $app->storagePath using the function
$app->useStoragePath($path_storage);

this will make the storage path to be same with the session, views, cache, logs

这将使存储路径与会话、视图、缓存、日志相同

回答by Vince Lowe

Laravel 5.3 is in bootstrap/app.php

Laravel 5.3 位于bootstrap/app.php

/*
|--------------------------------------------------------------------------
| Set Storage Path
|--------------------------------------------------------------------------
|
| This script allows us to override the default storage location used by
| the  application.  You may set the APP_STORAGE environment variable
| in your .env file,  if not set the default location will be used
|
*/

$app->useStoragePath( env( 'APP_STORAGE', base_path() . '/storage' ) );

回答by M Holod

Set it up in .env

在 .env 中设置

app.php

应用程序.php

'app_storage' => env('APP_STORAGE', storage_path()),

app/Providers/AppServiceProvider.php

应用程序/提供商/AppServiceProvider.php

public function register()
{
    $this->app->useStoragePath(config('app.app_storage'));
}

.env

.env

APP_STORAGE=custom_location

回答by BoogieBug

This works on Laravel 5.2

这适用于 Laravel 5.2

File: app/Providers/AppServiceProvider.php

文件: app/Providers/AppServiceProvider.php

public function register() {
  ...
  $this->app->useStoragePath(config('what_ever_you_want'));
  ...
}

回答by Hossein Shahdoost

Calling useStoragePathon your AppServiceProviderwouldn't do the job properly because the AppServiceProvideris called after the config files are loaded. so any use of storage_pathin config files would still refer to the old storage path.

调用useStoragePathAppServiceProvider不会正确完成工作,因为在AppServiceProvider加载配置文件后调用 。所以storage_path在配置文件中的任何使用仍然会引用旧的存储路径。

In order to properly solve this problem I suggest you extend Applicationclass and then on the constructor of your own class write the followings.

为了正确解决这个问题,我建议您扩展Application类,然后在您自己的类的构造函数上编写以下内容。


    /**
     * MyApplication constructor.
     */
    public function __construct($basePath = null)
    {
        parent::__construct($basePath);
        // set the storage path
        $this->afterLoadingEnvironment(function () {
            $this->useStoragePath(/*path to your storage directory*/);
        });
    }

回答by Rashid Iqbal

if your website is hosted;

如果您的网站是托管的;

move everything from public folder to root folder

将所有内容从公用文件夹移动到根文件夹

$app->bind('path.public', function() {
    return __DIR__;
});