问题
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 repository, which is fairly ugly I think. In Laravel 4 this was very simple with bootstrap/paths.php
.
In Laravel 5 it this works by using $app->useStoragePath('/path/')
in bootstrap/app.php
. However, I want to define the storage path with a config option, like $app->useStoragePath(config('app.storage_path')
. The config option calls an environment variable or returns a default location.
Doing this results in a Uncaught exception 'ReflectionException' with message 'Class config does not exist'
; this makes sense, because this function is not loaded yet.
I tried setting the storage path just after booting:
$app->booted(function () use ($app) {
$app->useStoragePath(config('app.storage_root'));
});
This changed nothing. I also tried directly binding it to path.storage
:
$app->bind('path.storage', function ($app) {
return config('app.storage_root');
});
The last option works partially; the view cache is now placed in the correct location, but the logs are still at the old location.
回答1:
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
回答2:
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' ) );
回答3:
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
回答4:
This works on Laravel 5.2
File: app/Providers/AppServiceProvider.php
public function register() { ... $this->app->useStoragePath(config('what_ever_you_want')); ... }
回答5:
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*/);
});
}
来源:https://stackoverflow.com/questions/31841392/change-the-storage-path-in-laravel-5