itsgoingd / slim-facades

"Static" interface for various Slim features
74 stars 9 forks source link

Issues creating custom "facade" #7

Closed eko3alpha closed 9 years ago

eko3alpha commented 10 years ago

I must be doing something wrong when attempting to create a custom facade. I have a SystemAlert class I'd like to create a Facade for and I have this.

$app->container->singleton('system_alert', function () {
    return new SystemAlert();
});
class SystemAlertFacade extends SlimFacades\Facade
{
    // return the name of the component from the DI container
    protected static function getFacadeAccessor() { return 'system_alert'; }
}
$config['aliases'] = array(
               'Alert'  => 'SystemAlert',
            );
SlimFacades\Facade::registerAliases($config['aliases']);

From my understanding this is all you need to do and I should be able to use the SystemAlerts class like so:

Alert::set(...);

Unfortunately it doesn't work. However all the following work:

$this->app->system_alert->set(); // inside a controller using DI $alert = new SystemAlert(); $alert = App:make('system_alert');

In my SystemAlert class I have several properties. If I instantiate the SystemAlerts class I can call the methods and they can access the properties without issues. However when I attempt to use the facade I get errors stating that my properties do not exist. I'm at a loss as to what I'm doing wrong.

Thanks!

ikhsan017 commented 10 years ago

on the aliases register, you should alias the facade class instead the underlying class

try this and see if it works

$app->container->singleton('system_alert', function () {
    return new SystemAlert();
});

class SystemAlertFacade extends SlimFacades\Facade
{
    // return the name of the component from the DI container
    protected static function getFacadeAccessor() { return 'system_alert'; }
}

$config['aliases'] = array(
               'Alert'  => 'SystemAlertFacade',
            );
SlimFacades\Facade::registerAliases($config['aliases']);
eko3alpha commented 10 years ago

Thank you! That worked perfectly!