1
我有一个门面(在这种情况下单),我就用ServiceProvider
注册:使用在Laravel外立面静态变量
服务提供商
use App;
class FacilityServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('Facility', function(){
return new Facility();
});
// Shortcut so developers don't need to add an Alias in app/config/app.php
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Facility', 'CLG\Facility\Facades\FacilityFacade');
});
}
}
门面
use Illuminate\Support\Facades\Facade;
class FacilityFacade extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'Facility'; }
}
现在,我想在我的Facility
类中有静态变量:
Facility.php
class Facility
{
public static $MODEL_NOT_FOUND = '-1';
public function __construct() { ... }
}
但是当我使用Facility::$MODEL_NOT_FOUND
,我得到Access to undeclared static property
。
我在做什么错?
是啊,我也这么认为。然后我不喜欢的是,我不得不把它称为函数,而不是变量(即'Facility :: getModelNotFound()'而不是'Facility :: $ ModelNotFound'或任何名称)。你有什么建议? – Kousha 2014-12-07 21:39:43
你不会在Laravel的Facade类中调用一个函数。但正如我所说的。你可以自己写。给我一些时间...我要写一个并更新答案。 – lukasgeiter 2014-12-07 21:42:21
呃呃...我认为这可能会像'__getStatic()',但显然没有这样的事情。你必须使用方法或根本没有Facade:/ – lukasgeiter 2014-12-07 21:59:51