Lumen - Change default Storage path

北城以北 提交于 2019-12-12 19:05:44

问题


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.

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

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

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

  • It should not involve the sysadmin (sym links, changing permissions, etc.)
  • Not tampering with the vendor folder.

回答1:


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

The key here is this line:

if (! function_exists('storage_path'))

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.

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. 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) {
        //
    }
    
    ....
    


来源:https://stackoverflow.com/questions/38384749/lumen-change-default-storage-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!