fogley's avatar

Accessing config from autoloaded helper file

I have a helpers.php file in which I need to access config variables, using the config() helper function. The file is autoloaded from composer.json...

"autoload": {
	"files": [
		"app/helpers.php"
	]
}

config() always return null. How would I go about making this happen?

0 likes
1 reply
kashyapH's avatar

Instead of calling config() at file load time, put them inside a function so they’re only called after the app has booted:

// app/helpers.php

if (!function_exists('myAppName')) {
    function myAppName()
    {
        return config('app.name'); // works fine at runtime
    }
}

This way config('...') runs only when you call myAppName(), not when Composer autoloads the file.

Please or to participate in this conversation.