2

I'm creating a package that uses internally this hashid package.

How can I register a third party facade inside a custom package?

I tried three options and none of them worked.

  1. Version - Composer

    "aliases": {
         "Hashids": "Vinkla\\Hashids\\Facades\\Hashids"
    }
    
  2. Version - inside my ServiceProvider with alias

     class MyPackageServiceProvider extends ServiceProvider
     {
        public function register()
        {
            ...
    
            $this->app->alias(\Vinkla\Hashids\Facades\Hashids::class, 'Hashids');
        } 
    
  3. Version - inside my ServiceProvider with AliasLoader

     class MyPackageServiceProvider extends ServiceProvider
     {
    
        public function register()
        {
            ...
    
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('Hashids', \Vinkla\Hashids\Facades\Hashids::class);
         }
    

When I'm testing the code, I get the error:

Error: Call to undefined method Vinkla\Hashids\Facades\Hashids::encode()

inside

/** @test */
public something_to_test()
{
    dd(\Hashids::encode(1));
}
Philipp Mochine
  • 4,351
  • 10
  • 36
  • 68

1 Answers1

1

Ok I found one solution, but I am still confused why it is like that.

In my "MyPackageServiceProvider" I need to add:

$this->app->register(HashidsServiceProvider::class);

Why do I need to register a Provider? I thought the composer is handling the work.

At the end only this works:

$this->app->register(HashidsServiceProvider::class);
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Hashids', \Vinkla\Hashids\Facades\Hashids::class);

The other versions doesn't work as well :(

Philipp Mochine
  • 4,351
  • 10
  • 36
  • 68