Expansion pack development
- Discover Expansion Pack
- Select Sexual discovery extension package
- Resource file
- Extension package default configuration
- Routing
- Database migration
- Publish language pack
- View
- Rewrite expansion package view
- Command
Extension package development
- Introduction
- Discover expansion pack
- Service provider
- Resource file
- Command
- Public resource file
- Publish group file
Introduction
Extension packages are the main way to add functionality to Laravel. Extension packages can contain many useful features, such as the time processing extension package Carbon, or the extension package Behat that provides a complete BDD testing framework.
Of course, there are many types of expansion packs. Some extensions are standalone, meaning they can be used with any PHP framework. Carbon and Behat are such standalone expansion packs. To use this extension package in Laravel, you only need to introduce them in the
composer.json
file.On the other hand, some extension packages can only be used in Laravel. These extension packages may contain files specifically designed to enhance your Laravel application's routes, controllers, views, and configuration. This guide mainly introduces the development of Laravel extension packages.
Facades Notes
When developing Laravel applications, there is usually no difference between using contracts or facades. Because they all provide essentially the same testability capabilities. However, when developing extension packages, the extension package does not have access to all test helper functions provided by Laravel. If you want to write test cases for an extension package like you would in a Laravel application, you can use the extension package Orchestral Testbench.
Discover the extension package
In the
config/app.php
configuration file of the Laravel application, theproviders
option defines the list of service providers that can be loaded by Laravel . When someone installs your extension, you need to include your service provider in this list. Instead of having users manually add your service providers to the list, you can define your service providers into theextra
section of your extension package'scomposer.json
file. In addition to service providers, you can also list all the facades you want to register:"extra": { "laravel": { "providers": [ "Barryvdh\Debugbar\ServiceProvider" ], "aliases": { "Debugbar": "Barryvdh\Debugbar\Facade" } } },
Once your extension package is configured to be discoverable, Laravel will automatically register the service providers and facades of the extension package during installation. Provide a user-friendly installation experience for extension pack users.
Selective discovery of extension packages
If you are an extension package user and want to prevent an extension package from being discovered, you can do so in the application's
composer The
extrasection of the .json
file lists this extension:"extra": { "laravel": { "dont-discover": [ "barryvdh/laravel-debugbar" ] } },
You can also use ## in your application's
dont-discover
directive #*characters, disable the extension package discovery function:
"extra": { "laravel": { "dont-discover": [ "*" ] } },
##Service ProviderService Provider Connect your extension with Laravel. The service provider is responsible for binding something to Laravel's service container and telling Laravel where to load the resource files of the extension package, such as views, configuration files, language packs, etc.
The service provider inherits the
Illuminate\Support\ServiceProviderclass and contains two methods:
register
andboot
. The base classServiceProvider
is located in the Composer extension package namedilluminate/support
, and you must add it to your extension package dependencies. To learn more about the structure and purpose of a service provider, consult its documentation.Resource FileConfigurationSometimes you need to publish the extension pack configuration file to the
configdirectory of the application itself . In this way, users using the extension package can easily override the default configuration items. To publish the expansion package configuration file, you only need to call the
vendor:publishpublishes
method in the service provider'sboot
method:
Now, when the user of the expansion package Execute Laravel's/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->publishes([ __DIR__.'/path/to/config/courier.php' => config_path('courier.php'), ]); }
command, and the extension package file will be copied to the specified directory. Of course, once your configuration file is published, it can be accessed like any other configuration:
{note} You should not define closure functions in configuration files. Because when the user executes the$value = config('courier.option');
config:cache
Artisan command, the configuration file will not be serialized correctly.
Extension package default configuration
You can also merge the extension package default configuration with the application's copy configuration. This allows extension pack users to define the configuration options they want to override in the replica configuration file. To merge configurations, just call the
mergeConfigFrom
method in the service provider'sregister
method:/** * 在容器中注册绑定。 * * @return void */ public function register(){ $this->mergeConfigFrom( __DIR__.'/path/to/config/courier.php', 'courier' ); }
{note} This method only merges Configure the first dimension of the array. If the extension user defines a multidimensional configuration array, missing options will not be merged.
Routing
If your extension package contains routing files, you need to use
The loadRoutesFrom
method loads them. This method will automatically determine whether the applied route has been cached. If the route has been cached, your route file will not be loaded:/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->loadRoutesFrom(__DIR__.'/routes.php'); }
##Database MigrationsIf your extension package contains database migrations, you need to use theloadMigrationsFrom
method to tell Laravel how to load them. The
loadMigrationsFrommethod only requires the extension pack migration file path as the only parameter:
/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->loadMigrationsFrom(__DIR__.'/path/to/migrations'); }
Once your extension pack migration files are registered, they will be used when running thephp artisan migrate
Language packIf your extension package contains language pack files, you need to usecommand will be executed automatically. You do not need to import them into your application's
database/migrationsdirectory.
loadTranslationsFrom
method tells Laravel how to load them. For example, if your extension package is named
courier, you need to add the following content to the service provider's
bootmethod:
/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->loadTranslationsFrom(__DIR__.'/path/to/translations', 'courier'); }
Extension package translation convention Use thepackage::file.line
syntax to reference. Therefore, you can load the
welcomeline of the
messagesfile in the
courierextension package as follows:
echo trans('courier::messages.welcome');
Publish language packIf you want to publish the language pack in the extension package to the application'sresources/lang/vendor
directory, you can use the service provider's
publishesmethod.
publishesThe method receives an array containing the language pack path and the corresponding publishing location. For example, to publish the language pack file of the
courierextension package, the operation is as follows:
/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->loadTranslationsFrom(__DIR__.'/path/to/translations', 'courier'); $this->publishes([ __DIR__.'/path/to/translations' => resource_path('lang/vendor/courier'), ]); }
Now, when the user of the extension package executes Laravel'svendor:publish
Artisan command, the language The package will be published to the specified directory.
View
If you want to register the view of your extension package in Laravel, you need to tell Laravel the location of the view file. You can do this using the service provider's
loadViewsFrom
method. TheloadViewsFrom
method allows receiving two parameters: view template path and extension package name. For example, if your extension package is namedcourier
, you need to add the following content to the service provider'sboot
method:/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->loadViewsFrom(__DIR__.'/path/to/views', 'courier'); }
Extension package view convention Use the
package::view
syntax to reference. Therefore, once the view path is successfully registered with the service provider, you can use the following method to load theadmin
view in thecourier
extension package:Route::get('admin', function () { return view('courier::admin'); });
Overriding extension package views
When you use the
loadViewsFrom
method, Laravel actually registers views in two places: the application'sresources/views/vendor
directory and your custom directory. So, taking thecourier
extension package as an example, Laravel will first check whether the developer has provided a customized version of the view inresources/views/vendor/courier
. Then, if the view has not been defined yet, Laravel will search the view directory defined inloadViewsFrom
. This approach allows users to easily customize or override the views of the extension package.Publish Views
If you wish to publish your views to your application's
resources/views/vendor
directory, you can use a service provider Thepublishes
method. Thepublishes
method receives an array containing the view path and the corresponding publishing location:/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->loadViewsFrom(__DIR__.'/path/to/views', 'courier'); $this->publishes([ __DIR__.'/path/to/views' => resource_path('views/vendor/courier'), ]); }
Now, when the user of the extension package executes Laravel's
vendor:publish
Artisan command, the view Will be published to a custom directory.Commands
If you want to register the Artisan command of the extension package in Laravel, you need to use
commands
method. This method receives an array of command classes. Once these commands are successfully registered, they can be executed using the Artisan command line:/** * 引导应用服务。 * * @return void */ public function boot(){ if ($this->app->runningInConsole()) { $this->commands([ FooCommand::class, BarCommand::class, ]); } }
Public Resource File
yours There may be resource files such as JavaScript, CSS, and images in the extension package. To publish these resource files to the application's
public
directory, you can use the service provider'spublishes
method. In the following example, we can also add apublic
resource classification tag, which can be used to classify related published resources:/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->publishes([ __DIR__.'/path/to/assets' => public_path('vendor/courier'), ], 'public'); }
Now, when the user of the extension package executes
vendor: publish
command, your resource files will be copied to the specified directory. Since resource files usually need to be overwritten every time an expansion pack is updated, the--force
tag is required:php artisan vendor:publish --tag=public --force
Publish group files
You may want to package and publish expansion pack resource files or resources separately. For example, you might want users to publish configuration files in an extension package individually, rather than being forced to publish all resource files in an extension package. You can "tag" different files by calling the
publishes
method in the extension package service provider. For example, let's use theboot
method in the extension package service provider to define two publishing groups:/** * 在注册后启动服务。 * * @return void */ public function boot(){ $this->publishes([ __DIR__.'/../config/package.php' => config_path('package.php') ], 'config'); $this->publishes([ __DIR__.'/../database/migrations/' => database_path('migrations') ], 'migrations'); }
Now your users can do this by executing
vendor:publish
Command to publish different group files based on defined tags:php artisan vendor:publish --tag=config
This article was first published on the LearnKu.com website.