Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mkfizi's avatar

Accessing config file from another config file under same subfolder

I'm trying to access a config file value from another config file under same subfolder.

config
|--subfolder
   |--file1.php
   |--file2.php

file1.php

return[
 'example1' => 'value'
]

file2.php

return[
 'example2' => config('subfolder.file1.example1')
]

config('subfolder.file1.example1') in config/subfolder/file2.php will return null. are there any valid way to retrieve the value other than putting file1.php into another subfolder or root config folder?

0 likes
5 replies
Sinnbeck's avatar

What is the use case? I cannot think of any case where this would be needed.

Could they both just read the same env value?

mkfizi's avatar

Be warned what i'm trying to do is not conventional at all.

I'm trying to store tailwindcss class names in the config files. This is so that I can make a dynamic component. And some of the components may shares the same class name. Let me demonstrate further:

Config/Theme/Component.php

return[
	'text' => [
		'size' => 'text-sm'
	]
]

Config/Theme/Component.php

return[
	'componentA' => [
		'color' => 'bg-gray-500',
		'size' => config('theme.global.text.size')	// currently returns null
	] ,
	'componentB' => [
		'color' => 'bg-red-500',
		'size' => config('theme.global.text.size')	// currently returns null
	] 
]

I want both componentA and componentB size to use the same value declared from global.php so if i want to change the size of bot component, I will have to just change it from global.php without the need to change the size value one by one in component.php. Further use case is as below:

App/Views/Component/ComponentA.php

public $size;
public function __construct(){
	$this->size = config('theme.component.text.size');
}

Should i decided that ComponentA uses its own size instead of the size from global.php, I would just need to change the size value in component.php.

These are the methods that I've tried previously:

Method 1: Inject the size value from global.php into the component itself as below:

App/Views/Component/ComponentA.php

public $size;
public function __construct(){
	$this->size = config('theme.global.text.size');
}

Cons: should i decided that ComponentA uses its own size instead of the size from global.php, I would need to change $size declaration in ComponentA.php from global.php to component.php. Configurations should be done only in configuration files.

Method2: Instead of grouping global.php or component.php under the same subfolder, just put them into separate subfolder as below:

config
|--Theme
|  |--global.php
|--Component
   |--style.php

Or just put them in root config folder as below:

config
|--global.php
|--style.php

Cons: Not an eye candy since their purpose are the same and there's lot of configuration file in between global.php and style.php.

The reason im storing these class names in config file is so that I can easily access them by just using helper function config(). I've tried storing the classes in json and yaml but that would result with excessive use of if else/switch statement.

mkfizi's avatar

@tykus i was actually aiming to strictly only use stylization from tailwindcss. i guess i might have to reconsider my life choice

mkfizi's avatar
mkfizi
OP
Best Answer
Level 1

Update:

I using an alternative where I simply include both data for 'global' and 'component' in single theme.php as below:

$global = [
  'text' => 'text-sm'
]

$component = [
  'text' => $global['text']
]

return [
  'global' => $global,
  'component' => $component
]

Please or to participate in this conversation.