CodeGuardian
CodeGuardian

Reputation: 21

Lumen - Change default Storage path

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?:

Upvotes: 2

Views: 7039

Answers (1)

maiorano84
maiorano84

Reputation: 11971

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

Upvotes: 7

Related Questions