dqaq59269 2014-06-06 10:33
浏览 23

Laravel 4.1 - 从服务提供商的启动功能调用时,Facade无法正常工作

I am having an issue getting a facade to work when it is used in the scope of a service provider's boot function. Example code is below.

Class

namespace Vendor\Libraries;

class Foo {
    public function bar()
    {
        echo 'bar';
    }
}

Facade

namespace Vendor\Facades;

use Illuminate\Support\Facades\Facade;

class Foo extends Facade
{
    protected static function getFacadeAccessor() { return 'foo'; }
}

Service Provider

namespace Vendor\Package;

use Illuminate\Support\ServiceProvider;

class MyServiceProvider extends ServiceProvider {

    public function boot()
    {
        $this->package('vendor/package');
    }

    public function register()
    {
        $this->app['foo'] = $this->app->share(function ($app) {
                return new \Vendor\Libraries\Foo;
        });
        $this->app->booting(function () {
                $loader = \Illuminate\Foundation\AliasLoader::getInstance();
                $loader->alias('Foo', 'Vendor\Facades\Foo');
        });
    }
}

This works great and I can use the Facade as expected, except in the one situation when I try to use it within the scope of another package’s service provider’s boot function. Example:

namespace OtherVendor\OtherPackage;

use Illuminate\Support\ServiceProvider;

class AnotherServiceProvider extends ServiceProvider {

    public function boot()
    {
        $this->package('othervendor/overpackage');
        \Foo::bar();
    }

    public function register()
    {
    }
}

Results in the error:

Class 'Foo' not found

Oddly, if I add the alias to the Alias array in the app.php file the the facade works.

'aliases' => array(
    'Foo' => 'Vendor\Facades\Foo',
)

I can also get it to work by using $this->app['foo']->bar() to call the function instead of using Foo::bar(); however if the bar() function uses a facade in the scope of it's body then I get an error saying that that class can't be found.

In summary it seems that if I try to add the alias using the AliasLoader class, as opposed to adding it to the Alias array in the app.php, then the facade wont work when called within the scope of another service providers boot function. It works everywhere else though as expected.

Any help on this issue would be great.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongyong6428 2015-01-22 06:58
    关注

    This is an older post but I believe boot() is called BEFORE register(). So the boot() in AnotherServiceProvider is called before the register() in MyServiceProvider. From my understanding anyway. I think that's the main difference between register() and boot(). So placing what's in register() of class MyServiceProvider to boot() and see if it works.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值