Downloads
Stars
Version
Refactor variables that are meant to be global out of your source code.
This is my first package.
Via Composer
$ composer require simongenin/laravelvariables
Publish the config file were your global variables will live
$ php artisan vendor:publish --tag=variables.config
It does the same thing as the config helper, with more ways to call it.
The goal here was to have a strict separation for variables that are global, but don't belong in another configuration file.
Once installed, you will have a new configuration file, called "variables.php". Just like any other config file, you can define many values by using nested arrays.
Here's an exemple of what it could be used for:
<?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', '[email protected]')
]
];
And here is how you can retrieve the values in your code:
/*
* 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