流明-更改默认存储路径 [英] Lumen - Change default Storage path

查看:105
本文介绍了流明-更改默认存储路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何在Lumen项目中更改默认存储位置(包括其子文件夹).由于多种原因,考虑到生产Web服务器的当前配置,Lumen在尝试写入日志编译Blade视图时会抛出权限被拒绝的异常.

I'm trying to find out how to change the default storage location (including it's subfolders) on a Lumen project. For several reasons, given the current configuration of the production web server, Lumen throws a permission denied exception when trying to write logs or compile Blade views.

唯一不涉及sysadmin的方法是将存储文件夹移动到Web服务器上的tmp文件夹.

The only alternative, without involving the sysadmin, is to move the storage folder to a tmp folder on the webserver.

在laravel上,似乎有一个名为" useStoragePath "的方法,但在流明(5.2.x)上似乎不可用.

On laravel there seems to be a method called "useStoragePath", but it doesn't seem to be available on Lumen (5.2.x).

默认路径似乎是硬编码的",我发现了这一点:

The default paths seem to be "hardcoded", I found this:

Project\vendor\laravel\lumen-framework\src\Application.php

/**
     * Get the storage path for the application.
     *
     * @param  string|null  $path
     * @return string
     */
    public function storagePath($path = null)
    {
        return $this->basePath().'/storage'.($path ? '/'.$path : $path);
    }

对于日志(相同文件):

And for the logs (same file):

/**
     * Get the Monolog handler for the application.
     *
     * @return \Monolog\Handler\AbstractHandler
     */
    protected function getMonologHandler()
    {
        return (new StreamHandler(storage_path('logs/lumen.log'), Logger::DEBUG))
                            ->setFormatter(new LineFormatter(null, null, true, true));
    }

底线:是否有任何牢记此限制的干净方法来覆盖默认存储路径?:

  • 它不应涉及sysadmin(符号链接,更改权限等)
  • 不篡改供应商文件夹.

推荐答案

在vendor/laravel/lumen-framework/src/helpers.php的第286行:

if (! function_exists('storage_path')) {
    /**
     * Get the path to the storage folder.
     *
     * @param  string  $path
     * @return string
     */
    function storage_path($path = '')
    {
        return app()->storagePath($path);
    }
}

这里的关键是这一行:

if (! function_exists('storage_path'))

这意味着如果尚未定义名为storage_path的函数,则Lumen将使用其自己的实现.

That means if a function named storage_path hasn't already been defined, then Lumen will use its own implementation.

您所要做的只是简单地编写自己的函数即可返回自己的自定义路径.

All you have to do is simply write your own function that returns your own custom path.

由于Lumen的规则比Laravel少得多,因此如何执行完全取决于您.也就是说,我建议您通过以下方式进行操作:

Because Lumen has far fewer rules than Laravel, how you do this is entirely up to you. That said, I would suggest doing it the following way:

  1. 在应用目录下放置一个名为 helpers.php 的文件
  2. 将所有自定义帮助程序功能添加到此文件中,包括您自己的storage_path实现
  3. 确保在流明本身之前加载此文件.为此,您需要在作曲家的自动加载器之前放置require语句.可以在 bootstrap/app.php 下的第一行完成:

  1. Place a file called helpers.php under your app directory
  2. Add any and all custom helper functions into this file including your own storage_path implementation
  3. Make sure this file is loaded before Lumen itself. In order to do that, you need to place your require statement before composer's autoloader. This can be done at the very first line under bootstrap/app.php:

require_once __DIR__ . '/../app/helpers.php';
require_once __DIR__ . '/../vendor/autoload.php';

try {
    (new Dotenv\Dotenv(__DIR__ . '/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}

....

这篇关于流明-更改默认存储路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆