首页 文章

流明 - 更改默认存储路径

提问于
浏览
2

我是一个Lumen项目的'm trying to find out how to change the default storage location (including it's子文件夹 . 出于多种原因,在生成Web服务器的当前配置的情况下,当尝试写入日志或编译Blade视图时,Lumen会抛出权限被拒绝的异常 .

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

在laravel上似乎有一个名为“ useStoragePath ”的方法,但它似乎不适用于Lumen(5.2.x) .

默认路径似乎是“硬编码”,我发现:

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);
    }

对于日志(相同的文件):

/**
     * 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));
    }

Bottom line: Is there any clean way of overriding the default storage path keeping in mind this restrictions?:

  • 它不应该涉及sysadmin(sym链接,更改权限等)

  • 不篡改供应商文件夹 .

1 回答

  • 2

    On Line 286 of vendor/laravel/lumen-framework/src/helpers.php:

    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将使用自己的实现 .

    您所要做的就是编写自己的函数,返回自己的自定义路径 .

    因为Lumen的规则远远少于Laravel,所以你如何做到完全取决于你 . 也就是说,我建议按以下方式进行:

    • 在您的app目录下放置一个名为helpers.php的文件

    • 将任何和所有自定义帮助程序函数添加到此文件中,包括您自己的 storage_path 实现

    • 确保在Lumen本身之前加载此文件 . 为此,您需要在composer的自动加载器之前放置您的require语句 . 这可以在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) {
        //
    }
    
    ....
    

相关问题