Changing the storage path in Laravel 5

So changing the Storage path in Laravel 5 is apparently more difficult than it possibly should be.

Here’s how I solved it with @stauffermatt ‘s tutorial to start but appended to make sure the storage path is stored in Config per. server rather than hardcoded into your Git stored php files.

  1. First follow Matt Stauffer’s tutorial, and add an application override

https://mattstauffer.co/blog/extending-laravels-application

# File /app/Application
<!--?php namespace App;

class Application extends Illuminate/Foundation/Application
{
    /**
     * Get the path to the storage directory.
     *
     * @return string
     */
    public function storagePath()
    {
        return $this->basePath.'/theNewStorage';
    }
}

  1. Then change your `bootstrap/app.php to…
$app = new AppApplication(
realpath(__DIR__.'/../')
);
  1. NOW, my extra bit. Create a config/paths.example.php and add it to git. Then duplicate it as, config/paths.php, add that file to your .gitignore and change the path to the server’s storage path.
<!--?php 

return [

    // Url to the server's storage file
        // eg, 'storagePath' => "/var/www/storage",
       'storagePath' => base_path()."/storage",
];

  1. Alter you app/Application.php file to load in the Config Repo with your new config file
<!--?php namespace App;

use Illuminate/Config/Repository as Config;

class Application extends Illuminate/Foundation/Application
{
    /**
     * Get the path to the storage directory.
     *
     * @return string
     */
    public function storagePath()
    {
        $path = config_path(). '/paths.php';
        $items = include $path;

        $config = new Config($items);

        return $config->get('storagePath');
     }
}

There. Dunnit.

Author: Dan Course

Dan Course, E-Learning Development at Warwick University (WMG). Agile Scrum trained and full-stack development. Interested in E-learning, Democracy, Politics and Tech. All thoughts are mine and not of my employer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s