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.