simongenin/laravelvariables

将全局变量从源代码中重构出来

v1.0.0 2018-08-29 13:03 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:44:55 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

将全局变量从源代码中重构出来。

注意

这是我第一个包。

安装

通过 Composer

$ composer require simongenin/laravelvariables

发布存储全局变量的配置文件

$ php artisan vendor:publish --tag=variables.config

用法

它执行与 config 辅助函数相同的功能,但提供了更多调用方式。

这里的目的是为那些全局但不应属于其他配置文件的变量提供一个严格的分离。

安装后,您将拥有一个名为 "variables.php" 的新配置文件。就像任何其他配置文件一样,您可以通过嵌套数组定义许多值。

以下是一些使用示例

<?php

return [

    /**
     * Define variables that have a reason to be global
     */
    
    'subscription' => [
        'monthly' => 19,
        'yearly' => 199,
        'lifetime' => 299
    ],

    'subtitle' => [
        'ATest' => 'A nice little package!',
        'BTest' => 'A wonderful little package!',
    ],

    'me' => [
        'email' => env('PERSONAL_CONTACT_MAIL', 'no@hotmail.com')
    ]

];

以下是在代码中检索值的方法

/*
 * There are several way you can ask for a variable
 */
$caller = new SimonGenin\LaravelVariables\LaravelVariables();
$cost = $caller->get('subscription.yearly');
$cost = $caller->__v('subscription.yearly');
$cost = $caller('subscription.yearly');

/*
 * You can use the config helper
 */
$cost = config('variables.subscription.lifetime');

/*
 * You can use the app helper to get the singleton instance
 */
$caller = app('variables');
$cost = $caller('subscription.monthly');
$cost = app('variables')->get('subscription.monthly');
$cost = app('variables')('subscription.monthly');

/*
 * You can call it dynamically by the resource name in camel case
 */
$caller = new SimonGenin\LaravelVariables\LaravelVariables();
$cost = $caller->subscriptionYearly();

/*
 * Same, but partly method call name and partly arguments
 */
$caller = new SimonGenin\LaravelVariables\LaravelVariables();
$cost = $caller->subscription('yearly');

/**
 * The two last methods are fancy, but a hell regarding project management.
 * Don't abuse it, especially on big projects.
 */

许可证

MIT