I'm trying to better understand how Services Container works in Laravel. So I know that Services are used to have something that you want to use set up by Laravel, right?
I'm trying to make a simple example using LDAP php built-in functions. I have this in my AppServiceProvider.php
:
public function register()
{
$this->app->bind('ldap', function() {
$conn = ldap_connect(env('LDAP_HOST'));
ldap_bind($conn, env('LDAP_BIND'), env('LDAP_PWD'));
return $conn;
});
}
Then in my controller I'm trying to $ldap = resolve('ldap');
but it's not working, it says resolve
doesn't exist. I already tried with $this->app->make
. How will I get my LDAP connection back?
The resolve()
helper method was not added until Laravel 5.3. It sounds like you're not using 5.3.
All the resolve()
helper method does is call the app()
helper method, anyway. You can continue to use the app()
helper method to resolve dependencies out of the container.
$ldap = app('ldap');
You can also use the App
facade, if you prefer that method.
$ldap = App::make('ldap');
You will only be able to use $this->app->make()
from your controller if you've injected the Application
container object in which your ldap
binding was registered.