Laravel:在哪里存储全局数组的数据和常量? [英] Laravel: Where to store global arrays data and constants?

查看:669
本文介绍了Laravel:在哪里存储全局数组的数据和常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始与Laravel合作.我需要使用Laravel 4作为基础框架来重写我几年前制作的整个系统.在我的旧系统中,我曾经有一个constant.php文件,其中声明了一些常量,以及一个globals.php文件,其中包含许多数组集(例如,类别状态,事件类型,语言等).这样,我可以使用类似的

I just started working with Laravel. I need to rewrite a whole system I made some years ago, using Laravel 4 as base framework. In my old system, I used to have a constant.php file with some constants declared, and a globals.php file which contained lots of array sets (for example, categories statuses, type of events, langs, etc.). By doing so, I could use something like

foreach ( $langs as $code => $domain ) {
    // Some stuff
}

我的应用中的任何地方.

anywhere in my app.

我的问题是,如何才能以所谓的"laravel方式"存储该信息.我尝试使用某种对象来存储此信息,将其设置为服务并为其创建外观:

My question is, how can I store that info in the so called "laravel way". I tried using some sort of object to store this info, setting this as a service and creating for it a facade:

app/libraries/Project/Constants.php

namespace PJ;

class Constants {

    public static $langs = [
            'es' => 'www.domain.es',
            'en' => 'www.domain.us',
            'uk' => 'www.domain.uk',
            'br' => 'www.domain.br',
            'it' => 'www.domain.it',
            'de' => 'www.domain.de',
            'fr' => 'www.domain.fr'
        ];
}

app/libraries/Project/ConstantsServiceProvider.php

namespace PJ;

use Illuminate\Support\ServiceProvider;

class ConstantsServiceProvider extends ServiceProvider {
    public function register() {
        $this->app->singleton('PJConstants', function() {
            return new Constants;
        });
    }
}

app/libraries/Project/ConstantsFacade.php

namespace PJ;

use Illuminate\Support\Facades\Facade;

class ConstantsFacade extends Facade {
    protected static function getFacadeAccessor() { 
        return 'PJConstants';
    }
}

composer.json

"psr-4": {
     "PJ\\": "app/libraries/Project"
},

,因此我以PJ\Constants::$langs的身份访问该属性.

and so I access that property as PJ\Constants::$langs.

这有效,但是我怀疑这是最有效或正确的方法.我的意思是,通过创建整个服务提供程序,facades和所有类似的东西来传播"变量的正确方法是吗?还是应该将这些数据放在哪里?

This works, but I doubt it is the most efficient or correct way of doing it. I mean, is it the right way to "propagate" a variable by creating a whole Service Provider and facades and all such stuff? Or where should I put this data?

谢谢您的建议.

EDIT#01

EDIT # 01

我想传递给所有控制器和视图的数据可以直接在脚本中设置,就像我文章开头的示例一样,但是它也可以从数据库中动态生成,用于例子.该数据可以是类别列表.我在所有视图中都需要它们来生成导航栏,但是我还需要它们来定义一些路由模式(例如/category/subcategory/product),并在多个控制器中解析一些信息(例如从保存X产品的类别中获取信息) .

Data I want to pass to all controllers and views can be directly set in script, like in the example at the beginning of my post, but it can also be generated dynamically, from a database for example. This data could be a list of categories. I need them in all views to generate a navigation bar, but I also need them to define some routing patterns (like /category/subcategory/product), and also to parse some info in several controllers (Like get info from the category that holds X product).

我的数组类似于:

$categories = [
    1 => ['name' => 'General', 'parent' => 0, 'description' => 'Lorem ipsum...'],
    2 => ['name' => 'Nature', 'parent' => 0, 'description' => 'Lorem ipsum...'],
    3 => ['name' => 'World', 'parent' => 0, 'description' => 'Lorem ipsum...'],
    4 => ['name' => 'Animals', 'parent' => 2, 'description' => 'Lorem ipsum...']
]

仅作为示例.索引是类别的ID,而值是与该类别关联的信息.

Just as an example. Index is the id of the category, and the Value is info associated with the category.

我也需要这个数组,在所有Controllers和Views中都可用.

I need this array, also, available in all Controllers and Views.

那么,我应该将其另存为Config变量吗?我还能如何存储这些数据?什么是最佳且语义正确的方法?

So, should I save it as a Config variable? How else could I store these data; what would be the best and semantically correct way?

推荐答案

对于在整个应用程序中全局使用的大多数常量,将它们存储在配置文件中就足够了.这也很简单

For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple

app/config目录中创建一个新文件.我们称之为constants.php

Create a new file in the app/config directory. Let's call it constants.php

您必须在其中返回一个配置值数组.

In there you have to return an array of config values.

return [
    'langs' => [
        'es' => 'www.domain.es',
        'en' => 'www.domain.us'
        // etc
    ]
];

您可以按以下方式访问它们

And you can access them as follows

Config::get('constants.langs');
// or if you want a specific one
Config::get('constants.langs.en');

您也可以设置它们

Config::set('foo.bar', 'test');

请注意,您设置的值将不会保留.它们仅适用于当前请求.

Note that the values you set will not persist. They are only available for the current request.

配置可能不是存储从数据库生成的信息的正确位置.您可以像这样使用口才模型:

The config is probably not the right place to store information generated from the database. You could just use an Eloquent Model like:

class Category extends Eloquent {
    // db table 'categories' will be assumed
}

并查询所有类别

Category::all();

如果由于某种原因导致整个Model问题无法解决,您可以开始考虑创建自己的类和外观.或者,您可以只创建一个包含所有静态变量和方法的类,然后在不使用外观的情况下使用它.

If the whole Model thing for some reason isn't working out you can start thinking about creating your own class and a facade. Or you could just create a class with all static variables and methods and then use it without the facade stuff.

这篇关于Laravel:在哪里存储全局数组的数据和常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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