Simply you may put something like this in your config file:
return [
'ip' => app('request')->ip()
];
I use a little customized one for my sitewise configs, for example, let's say you want to use something like this:
/**
* Get config/constants.php
*
* [
* 'person' => [
* 'name' => 'Me',
* 'age' => 1000
* ]
* ];
*/
$name = constants('person.name');
So, to achieve this you need to write a function like:
// Helpers/Common.php
function constants($key = null)
{
$constants = config('constants');
return is_null($key) ? $constants : array_get($constants, $key);
}
Now, in your composer.json
file you may add the following files
entry:
"psr-4": {
"App\\": "app/"
},
"files": ["Helpers/Common.php"]
Then you need to add the constants.php
in config
directory for example:
<?php
return [
"ip" => app('request')->ip(),
"person" => [
"name" => "Sheikh Heera",
"age" => 10000
],
];
Finally, just run composer-dump
from terminal and you are done. So, if the ip
key is available in the array then you may just try this:
$ip = constants('ip');
From the view (Blade), you may use following to echo out the ip
:
{{ constants('ip') }}
Let's sum up the whole process:
Create a directory in your project root (or inside app if you wish) as Helpers
.
Create the Common.php
file in that directory and put the array (return it)
Put the constants
function (given above) in the Common.php
file
Add the files
(given above) key in your composer.json
file
Run composer-dump
to update autoload files
That's it. Use the file name and helper function name that describes your domian, so instead of constants
you may use for example: site
or your domain name as well.