Skip to content
Advertisement

Create custom package with facade in Laravel 8

I am trying to develop a new Laravel package locally installed via Composer.

These are my steps:

  • I install a new Laravel application with composer create-project laravel/laravel my-application

  • I create a new directory in the root with the following content:

    packages/randolf/custom-package/composer.json

    {
        "name": "randolf/custom-package",
        "description": "My new Custom Package",
        "type": "library",
        "license": "MIT",
        "require": {},
        "autoload": {
            "psr-4": {
                "Randolf\CustomPackage\" : "src/"
            }
        },
        "extra": {
            "laravel": {
                "providers": [
                    "Randolf\CustomPackage\CustomPackageServiceProvider"
                ],
                "aliases": {
                    "CustomPackage": "Randolf\CustomPackage\Facades"
                }
            }
        }
    }
    

    packages/randolf/custom-package/src/CustomPackage.php

    <?php
    
    namespace RandolfCustomPackage;
    
    class CustomPackage
    {
        public function sayHi()
        {
            return "Hi from class!";
        }
    }
    

    packages/randolf/custom-package/src/CustomPackageServiceProvider.php

    <?php
    
    namespace RandolfCustomPackage;
    
    use IlluminateSupportServiceProvider;
    
    class CustomPackageServiceProvider extends ServiceProvider
    {
        public function boot()
        {
    
        }
    
        public function register()
        {
            $this->app->bind('custom-package', function()
            {
                return new CustomPackage();
            });
        }
    }
    

    packages/randolf/custom-package/src/Facades/CustomPackageFacade.php

    <?php
    
    namespace RandolfCustomPackageFacades;
    
    use IlluminateSupportFacadesFacade;
    
    class CustomPackageFacade extends Facade
    {
    
        /**
        * Get the registered name of the component.
        *
        * @return string
        */
        protected static function getFacadeAccessor() { return 'custom-package'; }
    }
    
  • I add my package in Laravel with composer, adding the repositories key: /composer.json

    "repositories": {
        "randolf/custom-package": {
            "type": "path",
            "url": "packages/randolf/custom-package",
            "options": {
                "symlink": true
            }
        }
    },
    "require": {
        ...
        "randolf/custom-package": "@dev"
    },
    
  • I run composer update and the install, package discover and dump-autoload works correctly:

    Loading composer repositories with package information
    Updating dependencies
    Lock file operations: 1 install, 0 updates, 0 removals
    - Locking randolf/custom-package (dev-master)
    Writing lock file
    Installing dependencies from lock file (including require-dev)
    Package operations: 1 install, 0 updates, 0 removals
    - Installing randolf/custom-package (dev-master): Junctioning from packages/randolf/custom-package
    Generating optimized autoload files
    > IlluminateFoundationComposerScripts::postAutoloadDump
    > @php artisan package:discover --ansi
    Discovered Package: facade/ignition
    Discovered Package: fideloper/proxy
    Discovered Package: fruitcake/laravel-cors
    Discovered Package: laravel/sail
    Discovered Package: laravel/tinker
    Discovered Package: nesbot/carbon
    Discovered Package: nunomaduro/collision
    Discovered Package: randolf/custom-package
    Package manifest generated successfully.
    73 packages you are using are looking for funding.
    Use the `composer fund` command to find out more!
    
  • I create a route in routes/web.php to test the facade:

    Route::get('/test-facade', function () {
        echo CustomPackage::sayHi();
    });
    

Result: laravel facade error

Advertisement

Answer

Adjust the alias in the composer.json to point to the Facade instead of its namespace:

"CustomPackage": "Randolf\CustomPackage\Facades\CustomPackageFacade"
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement