Laravel does in its core in Application.php register a lot of the Default Implementations like this:
'url' => ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'],
Which will in effekt call the method below two times
public function alias($abstract, $alias)
{
$this->aliases[$alias] = $abstract;
}
resulting in the following values in Container->aliases
:
"Illuminate\Routing\UrlGenerator" => "url"
"Illuminate\Contracts\Routing\UrlGenerator" => "url"
If I later call:
$this->app->alias('url', 'App\Util\Portal\UrlGenerator');
it even stores it a thrid time in the Container im the alias array:
"App\Util\Portal\UrlGenerator" => "url"
My Question:
Why does laravel store them all two or three and not override them? It should be sufficient to store the concrete class. But why does laravel store them all three? How can laravel tell, which one to resolve, when I now use App::make('url')? Laravel has now three to choose from, one interface and two implementations.
After Sleeping a night over it and digging more in the code (could yet not be 100% verified in code yet) its most likely as that:
"Aliasing"
Is used in various ways/methods:
"Binding"
Laravel Container has two properties in its Container class named $aliases
and $bindings.
Bindings hold the real binding of an "abstract" to a concrete class to instanciate!
So every alias (as discribed above) needs to also(!) have a corresponding binding(!) from the "abstract" the concrete class to be instanciated.
Conclusion
In effect as written above, there are thre aliases (of concrete classes and interfaces) to the key/abstract "url". But they have nothing to do with the instanciating process. For the alias to work there additinally also needs to be a real binding!
So in effect "Container Aliases" allow you to access an existing binding with other classses or interface names.
Container::make()
with any of the aliases, laravel will try to resolve them to the "abstract" (here "url").So you can have an arbitrary amount of aliases but only one binding and you have to have that single binding (additionally!).
(Interestingly, if you map an alias to the same class as the binding, it seems to end in an recursion error. But this also might be an xdebug problem).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With