更改Laravel 5中的存储路径 [英] Change the storage path in Laravel 5

查看:386
本文介绍了更改Laravel 5中的存储路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Laravel 5.1使用的存储路径更改为/home/test/storage之类的东西.这样做的好处是这些文件没有存储在存储库中,我认为这非常丑陋.在Laravel 4中,使用bootstrap/paths.php非常简单.

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.

在Laravel 5中,这可以通过使用bootstrap/app.php中的$app->useStoragePath('/path/')来实现.但是,我想使用config选项定义存储路径,例如$app->useStoragePath(config('app.storage_path'). config选项调用环境变量或返回默认位置.

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.

这样做会导致Uncaught exception 'ReflectionException' with message 'Class config does not exist';这很有意义,因为尚未加载此功能.

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'));
});

这没有改变.我也尝试将其直接绑定到path.storage:

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.

推荐答案

这是更改 Laravel 5 中存储路径的简单解决方案,就像我们在 Laravel 4 中所做的一样

Here is a simple solution of changing the storage path in Laravel 5 like we do in Laravel 4

在bootstrap/app.php上

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

这篇关于更改Laravel 5中的存储路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆