spa*_*33z 11 php laravel laravel-5.1
我想改变Laravel 5.1使用的存储路径/home/test/storage.这样做的好处是这些文件不会存储在存储库中,我认为这相当丑陋.在Laravel 4中,这非常简单bootstrap/paths.php.
在Laravel 5中,这可以通过使用$app->useStoragePath('/path/')来实现bootstrap/app.php.但是,我想用config选项定义存储路径,比如$app->useStoragePath(config('app.storage_path').config选项调用环境变量或返回默认位置.
这样做会产生一个Uncaught exception 'ReflectionException' with message 'Class config does not exist'; 这是有道理的,因为此功能尚未加载.
我尝试在启动后设置存储路径:
$app->booted(function () use ($app) {
$app->useStoragePath(config('app.storage_root'));
});
Run Code Online (Sandbox Code Playgroud)
这没有改变.我也试过将它直接绑定到path.storage:
$app->bind('path.storage', function ($app) {
return config('app.storage_root');
});
Run Code Online (Sandbox Code Playgroud)
最后一个选项部分起作用; 视图缓存现在放在正确的位置,但日志仍在旧位置.
在 .env 中设置
应用程序.php
'app_storage' => env('APP_STORAGE', storage_path()),
Run Code Online (Sandbox Code Playgroud)
应用程序/提供商/AppServiceProvider.php
public function register()
{
$this->app->useStoragePath(config('app.app_storage'));
}
Run Code Online (Sandbox Code Playgroud)
.env
APP_STORAGE=custom_location
Run Code Online (Sandbox Code Playgroud)
这是一个简单的解决方案,可以像在Laravel 4中一样更改Laravel 5中的存储路径
在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);
Run Code Online (Sandbox Code Playgroud)
这将使存储路径与会话,视图,缓存,日志相同
Laravel 5.3在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' ) );
Run Code Online (Sandbox Code Playgroud)
这适用于 Laravel 5.2
文件:app/Providers/AppServiceProvider.php
公共函数寄存器(){
...
$this->app->useStoragePath(config('what_ever_you_want'));
...
}