在 Laravel 4 中,要正确扩展一个类并创建一个新的 Facade,您必须:
基于另一个类创建新类:
<?php namespace AntonioRibeiro\UserManagementSystem;
class UMS extends \Cartalyst\Sentry\Sentry {
// For now your UMS class is identical to Sentry, so you must have nothing here, right?
}
创建一个 ServiceProvider,它将实例化您的类:
<?php namespace AntonioRibeiro\UserManagementSystem;
use Illuminate\Support\ServiceProvider;
class UMSServiceProvider extends ServiceProvider {
protected $defer = true;
public function register()
{
$this->app['ums'] = $this->app->share(function($app)
{
// Once the authentication service has actually been requested by the developer
// we will set a variable in the application indicating such. This helps us
// know that we need to set any queued cookies in the after event later.
$app['sentry.loaded'] = true;
return new UMS(
$app['sentry.user'],
$app['sentry.group'],
$app['sentry.throttle'],
$app['sentry.session'],
$app['sentry.cookie'],
$app['request']->getClientIp()
);
});
}
public function provides()
{
return array('ums');
}
}
创建立面:
<?php namespace AntonioRibeiro\UserManagementSystem;
use Illuminate\Support\Facades\Facade as IlluminateFacade;
class UMSFacade extends IlluminateFacade {
protected static function getFacadeAccessor() { return 'ums'; }
}
将您的服务提供者添加到 app/config/app.php 中的列表中:
'providers' => array(
...
'AntonioRibeiro\UserManagementSystem\UMSServiceProvider',
),
将 Facade 添加到 app/config/app.php 中的别名列表中:
'aliases' => array(
...
'UMS' => 'AntonioRibeiro\UserManagementSystem\UMSFacade',
),
更新自动加载的类:
composer du --optimze
并使用它:
var_dump( UMS::check() );
var_dump( UMS::getUser() );