Support the ongoing development of Laravel.io →
posted 4 months ago

Hello,

a little bit off-topic but I would like to understand what´s going wrong with my code. I would like to use a third-party extension outside of laravel; for this I need the config() helper to get it running.

Here is my code:

require __DIR__.'/../vendor/autoload.php';

use Illuminate\Container\Container;
use Illuminate\Config\Repository;
use Illuminate\Support\Facades\Config;

/*
 * from https://github.com/laravel/framework/blob/11.x/src/Illuminate/Foundation/helpers.php#L108
 */
	if (! function_exists('app')) {
	    /**
	     * Get the available container instance.
	     *
	     * @template TClass
	     *
	     * @param  string|class-string<TClass>|null  $abstract
	     * @param  array  $parameters
	     * @return ($abstract is class-string<TClass> ? TClass : ($abstract is null ? \Illuminate\Foundation\Application : mixed))
	     */
	    function app($abstract = null, array $parameters = [])
	    {
	        if (is_null($abstract)) {
	            return Container::getInstance();
	        }

	        return Container::getInstance()->make($abstract, $parameters);
	    }
	}

/*
 * from https://github.com/laravel/framework/blob/11.x/src/Illuminate/Foundation/helpers.php#L259
 */
	if (! function_exists('config')) {
	    /**
	     * Get / set the specified configuration value.
	     *
	     * If an array is passed as the key, we will assume you want to set an array of values.
	     *
	     * @param  array<string, mixed>|string|null  $key
	     * @param  mixed  $default
	     * @return ($key is null ? \Illuminate\Config\Repository : ($key is string ? mixed : null))
	     */
	    function config($key = null, $default = null)
	    {
	        if (is_null($key)) {
	            return app('config');
	        }

	        if (is_array($key)) {
	            return app('config')->set($key);
	        }

	        return app('config')->get($key, $default);
	    }
	}

$container = new \Illuminate\Container\Container();
Illuminate\Support\Facades\Facade::setFacadeApplication($container); 		// result: NULL
$container->singleton('app', Illuminate\Container\Container::class); 		// result: NULL
class_alias(Illuminate\Support\Facades\App::class, 'app');

$container->singleton('config', \Illuminate\Config\Repository::class);
class_alias(\Illuminate\Config\Repository::class, 'config');

/*
also tried without success:
$container->singleton('config', function () use ($container) {
  $config = new Illuminate\Config\Repository;
  return $config;
});
class_alias(\Illuminate\Config\Repository::class, 'config');
*/


// this is ok and returns "hello world"
Config::set('examplekey', 'hello world');
echo Config::get('examplekey', 'defaultValue');
echo "<br>\n";

// but this returns "defaultValue"
echo config('examplekey', 'defaultValue');
echo "<br>\n";

// seems that ->make is problematic (?) - each time it returns another instance id:
echo "<pre>";
Var_Dump(Container::getInstance()->make('config', []));
Var_Dump(Container::getInstance()->make('config', []));
echo "</pre><br>\n";

exit;

When I try to use the config-helper (1:1 taken from the laravel repository) I do not get my config-values. I have stripped it down: ->make (used by the 'app' function) creates every time a new instance. The expected behaviour is that a singleton is being used - therefore the config values should be present.

I guess I need some "glue" between the 'app' and the 'config' classes (?).

Hope someone could help.

Thanks Sebastian

Last updated by @slemke76 4 months ago.
0

Hello Sebastian, So from what I can see it seems you would want to use the Config class to overwrite what Laravel's config function. You can first try clearing and rebuilding the config cache using:

php artisan config:clear
php artisan config:cache

If that doesn't work then in the app service provider ensure you are binding the new custom configuration in the register function. Like this:

$this->app->singleton(ConfigRepository::class, function ($app) {
            return new CustomConfigRepository($app['config']->all());
        });
0

Hello,

thanks for your reply.

would want to use the Config class to overwrite what Laravel's config function

No, that´s not my goal. I would like to use the Illuminate\Config functions without Laravel (therefore artisan is also not available). (Therefore I wrote it´s perhaps a bit off-topic :-)

I do not understand why the code above does not use a singleton (it should) when using Container->[..]->make.

Thanks, Sebastian

0

Oh okay, I get you now. So from what I can gather the binding method you are using would always create a new instance. Also the same is done with the App. I would try this:

// Bind the container itself to the instance
$container->singleton('app', function () use ($container) {
    return $container;
});

// Correctly bind the `config` instance as a shared singleton
$container->singleton('config', function () {
    return new Illuminate\Config\Repository();
});

I also had a question. Is there a reason you are using the Repository for the `class_alias` instead of the Config ?

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.