Reputation:
Im trying to setup a facade for my Gravatar library.
The problem I have is the following,the ServiceProvider does not get registert. So 'gravatar' is not in the IoC.
I added \Log::info('initialized');
to my ServiceProvider but nothing is logged.
Call to undefined method Facade\Gravatar::getUrl()
Upvotes: 0
Views: 165
Reputation: 12293
So, a Facade takes (potential) three classes:
Gravatar::getUrl()
See here on creating facades in Laravel for some more info.
In your case, I don't see the code from point 1.
Libraries/Gravatar/Avatar.php
<?php namespace Gravatar;
class Avatar {
public function getUrl() { ... }
}
Libraries/Gravatar/Facade.php
<?php namespace Gravatar;
use Illuminate\Support\Facades\Facade as BaseFacade;
class Facade extends BaseFacade {
protected static function getFacadeAccessor() {
return 'gravatar';
}
}
Libraries/Gravatar/GravatarServiceProvider.php
<?php namespace Gravatar;
use Illuminate\Support\ServiceProvider
class GravatarServiceProvider extends ServiceProvider {
public function register() {
Log::info('initialized');
$this->app['gravatar'] = $this->app->share(function () {
return new Avatar();
});
}
}
app/config/app.php
<?php
'providers' => array(
...
'Gravatar\GravatarServiceProvider'
),
'aliases' => array(
...
'Gravatar' => 'Gravatar\Facade'
),
Note that I the namespace I used assumes this directory structure:
Libraries
Gravatar
Avatar.php
GravatarServiceProvider.php
Facade.php
With autoloading like this in composer.json
:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/tests/TestCase.php"
],
"psr-0": {
"Gravatar": "app/Library" // Or wherever your Library directory is
}
},
If you're confused on Namespacing and how that works with autoloading, read up on PSR-0.
Hope that helps!
Upvotes: 2