php - 在 Laravel 8 中创建带有门面的自定义包

标签 php laravel package laravel-facade

我正在尝试开发一个通过 Composer 在本地安装的新 Laravel 包。

这些是我的步骤:

  • 我使用 composer create-project laravel/laravel my-application 安装了一个新的 Laravel 应用程序

  • 我在根目录中创建了一个新目录,内容如下:

    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 Randolf\CustomPackage;
    
    class CustomPackage
    {
        public function sayHi()
        {
            return "Hi from class!";
        }
    }
    

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

    <?php
    
    namespace Randolf\CustomPackage;
    
    use Illuminate\Support\ServiceProvider;
    
    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 Randolf\CustomPackage\Facades;
    
    use Illuminate\Support\Facades\Facade;
    
    class CustomPackageFacade extends Facade
    {
    
        /**
        * Get the registered name of the component.
        *
        * @return string
        */
        protected static function getFacadeAccessor() { return 'custom-package'; }
    }
    
  • 我使用 composer 在 Laravel 中添加我的包,添加 repositories 键: /composer.json

    "repositories": {
        "randolf/custom-package": {
            "type": "path",
            "url": "packages/randolf/custom-package",
            "options": {
                "symlink": true
            }
        }
    },
    "require": {
        ...
        "randolf/custom-package": "@dev"
    },
    
  • 我运行 composer update 并且安装、包发现和转储自动加载工作正常:

    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
    > Illuminate\Foundation\ComposerScripts::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!
    
  • 我在 routes/web.php 中创建一个路由来测试外观:

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

结果: laravel facade error

最佳答案

调整 composer.json 中的别名指向 Facade 而不是它的命名空间:

"CustomPackage": "Randolf\\CustomPackage\\Facades\\CustomPackageFacade"

关于php - 在 Laravel 8 中创建带有门面的自定义包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65407519/

相关文章:

php - Twig - 访问用户对象 ID

r - Windows 上的 R 包检查问题 : package suggested but not available: 'stringi'

php - Laravel 函数 Controller 验证错误

php - Laravel 5.5 导出 EXCEL

php - 如何使 Laravel 中的特定 session 无效(用户使用记住我功能)

python - 无法在 Python 中打开 html5lib

javascript - 如何在 Lerna 包中共享构建脚本

php - MySQL 无法看到 PHP 中的列。确定为 SQL 语句

php - IntlDateFormatter::format():datefmt_format:采用数组或整数时间戳值或 DateTime 对象 (SonataSanBox)

php - 将实现绑定(bind)到 Laravel 中的请求/响应?