在 Laravel 8 中创建带有外观的自定义包
Create custom package with facade in Laravel 8
我正在尝试开发一个新的 Laravel 包,通过 Composer 在本地安装。
这些是我的步骤:
我用 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"
},
I 运行 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();
});
结果:
调整 composer.json
中的别名以指向 Facade 而不是它的命名空间:
"CustomPackage": "Randolf\CustomPackage\Facades\CustomPackageFacade"
我正在尝试开发一个新的 Laravel 包,通过 Composer 在本地安装。
这些是我的步骤:
我用
安装了一个新的 Laravel 应用程序composer create-project laravel/laravel my-application
我在根目录新建一个目录,内容如下:
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" },
I 运行
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(); });
结果:
调整 composer.json
中的别名以指向 Facade 而不是它的命名空间:
"CustomPackage": "Randolf\CustomPackage\Facades\CustomPackageFacade"