Feb 14, 2022
0
Level 1
Retrieve values from custom config file in a public directory
I've created a custom helper which works like config() but it retrieve the file from public directory instead
if (! function_exists('theme')) {
function theme(string $target)
{
$keys = explode('.', $target);
$themeValues = include(public_path('theme').'/'.$keys[0].'.php');
array_shift($keys);
foreach ($keys as $index => $key) {
$themeValues = array_intersect_key(
$themeValues, array_flip([$key])
)[$key];
}
return $themeValues;
}
}
Files in public/theme:
//global.php
return[
'component' => [
'item' => 'example'
]
];
//resources.php
return[
'vendor' => [
'config' => [
'item => 'resource'
]
]
];
Calling method:
theme('global.component.item'); // outputs 'example'.
theme('resources.vendor.config.item'); // outputs 'resource'.
theme('resources.vendor.config') // outputs 'array:1 'item' => 'resource'
Need a feedback in case I missed anything crucial
Please or to participate in this conversation.