在 Laravel 5.1 中使用 .env 设置的存储路径

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

Storage path set using .env in Laravel 5.1

phplaravellaravel-5phpdotenv

提问by Mic Jaw

I want to configure the storage path in a Laravel 5.1 using the .envfile. My bootstrap/app.phplooks like this:

我想使用该.env文件在 Laravel 5.1 中配置存储路径。我的bootstrap/app.php看起来像这样:

<?php
$app = new \Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);
$app->useStoragePath(getenv('STORAGE_PATH'));

and the relevant line in .env file is:

.env 文件中的相关行是:

STORAGE_PATH=/var/www/storage

This doesn't work. I figured out the Dotenv library is initialised afterthe bootstrap is processed so the .envvariables are not available in bootstrap.php.

这不起作用。我发现 Dotenv 库是处理引导程序初始化的因此.env变量在bootstrap.php.

Is there a different place where I can set the storage path and the .envvariables are available?

有没有其他地方可以设置存储路径并且.env变量可用?

回答by MartinJH

In config/filesystems.phpyou can set your storage path. Try setting your storage path there and see if it works. Note that the example below is my suggestion as to how your config/filesystems.phpshould look. Don't mind the s3 setup. That's a part of my project.

config/filesystems.php您可以设置您的存储路径。尝试在那里设置您的存储路径,看看它是否有效。请注意,下面的示例是我对您的config/filesystems.php外观的建议。不要介意 s3 设置。那是我项目的一部分。

Remember to remove $app->useStoragePath(getenv('STORAGE_PATH'));from app.php

记得$app->useStoragePath(getenv('STORAGE_PATH'));app.php

return [

    'default' => 's3',

    'cloud' => 's3',

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root'   => env('STORAGE_PATH'),
        ],

        's3' => [
            'driver' => 's3',
            'key'    => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

        'rackspace' => [
            'driver'    => 'rackspace',
            'username'  => 'your-username',
            'key'       => 'your-key',
            'container' => 'your-container',
            'endpoint'  => 'https://identity.api.rackspacecloud.com/v2.0/',
            'region'    => 'IAD',
        ],
    ],
];