I want to change the storage path Laravel 5.1 uses to something like /home/test/storage
. This has the advantage that these files are not stored in the repositor
Laravel 5.3 is in bootstrap/app.php
/*
|--------------------------------------------------------------------------
| Set Storage Path
|--------------------------------------------------------------------------
|
| This script allows us to override the default storage location used by
| the application. You may set the APP_STORAGE environment variable
| in your .env file, if not set the default location will be used
|
*/
$app->useStoragePath( env( 'APP_STORAGE', base_path() . '/storage' ) );
This works on Laravel 5.2
File: app/Providers/AppServiceProvider.php
public function register() { ... $this->app->useStoragePath(config('what_ever_you_want')); ... }
Calling useStoragePath
on your AppServiceProvider
wouldn't do the job properly because the AppServiceProvider
is called after the config files are loaded. so any use of storage_path
in config files would still refer to the old storage path.
In order to properly solve this problem I suggest you extend Application
class and then on the constructor of your own class write the followings.
/**
* MyApplication constructor.
*/
public function __construct($basePath = null)
{
parent::__construct($basePath);
// set the storage path
$this->afterLoadingEnvironment(function () {
$this->useStoragePath(/*path to your storage directory*/);
});
}
Set it up in .env
app.php
'app_storage' => env('APP_STORAGE', storage_path()),
app/Providers/AppServiceProvider.php
public function register()
{
$this->app->useStoragePath(config('app.app_storage'));
}
.env
APP_STORAGE=custom_location
if your website is hosted;
move everything from public folder to root folder
$app->bind('path.public', function() {
return __DIR__;
});
Here is a simple solution of changing the storage path in Laravel 5 like we do in Laravel 4
on bootstrap/app.php
# new storage path
# base_path() -> returns root path
$path_storage = base_path() . "../../storage";
# override already $app->storagePath using the function
$app->useStoragePath($path_storage);
this will make the storage path to be same with the session, views, cache, logs