Unresolvable dependency resolving [Parameter #0 [$name ]]
When refactoring a Laravel application from Facades to explicit dependency injection, encountering Illuminate \ Container \ BindingResolutionException is common. The error message itself, "Unresolvable dependency resolving [Parameter #0 [
To debug this effectively, you must stop looking at the file where the exception was thrown and start inspecting the constructor of the class being instantiated. The container builds objects recursively; if Class A requires Class B, and Class B is not registered in the container, the container throws an error while trying to resolve Class A.
In the scenario described, the application has a HelpersServiceProvider that registers the Acme\Services\Helpers class under the alias 'helpers'. However, this service provider attempts to instantiate Helpers in its register() method using hardcoded bindings:
$this->app->bind('helpers', function () {
return new Helpers(
$this->app['config'],
$this->app['translator'],
// ... other dependencies
);
});
This approach works fine if every single dependency passed into the Helpers constructor is already bound in the container. If any of those dependenciesâsuch as Acme\Interfaces\MockablyInterface or a specific database connectionâare missing from the container, the BindingResolutionException fires immediately when the container tries to execute the closure in the register() method.
The solution depends entirely on whether the missing dependency is intended to be a singleton (a global instance shared by all requests) or a transient object (created per request).
Registering Singleton Bindings
If MockablyInterface is a utility class that should exist globally, you must ensure it is bound in the container. In older versions of Laravel (like version 4 mentioned in the source), bindings were often added to the app/start/global.php file or within service providers.
To fix the error for MockablyInterface, add a binding similar to the existing HelpersInterface registration:
// app/start/global.php or inside a Service Provider register() method
App::bind('Acme\Interfaces\MockablyInterface', 'Acme\Services\MockableImplementation');
Once this binding exists, the container can resolve MockablyInterface from the closure in the HelpersServiceProvider, and the exception will disappear.
Using Closure Bindings for Transients
If you intend to create a new instance of the missing dependency every time Helpers is instantiated, you should not use App::bind. Instead, keep the constructor argument as a type hint and let Laravel resolve it automatically. However, this requires that the transient dependencies are themselves resolvable by the container.
For example, if $this->app['mockably'] is accessed but no binding exists for 'mockably' or Acme\Interfaces\MockablyInterface, you must define one. Laravel's default behavior is to look for a class with the exact name of the dependency string or an alias if one exists.
The Trap of Manual Instantiation
The most common pitfall in these refactoring scenarios is manually instantiating complex classes inside a register() method closure while assuming all dependencies are present. This turns your service provider into a manual wiring harness rather than leveraging the container's inversion of control.
If you find yourself repeatedly typing out new instances of services inside closures, consider moving the instantiation logic to the constructor of the class itself (if it's a singleton) or ensuring that every single dependency is properly registered either as an alias or a concrete binding.
By systematically checking which dependencies are passed into the failing constructor and ensuring each one has a corresponding entry in the container (either via App::bind, a facade replacement, or an existing alias), you can resolve the "Unresolvable dependency" error without needing to guess where the problem lies.
