Packages is the most important way to add functionality to the Laravel. All the require in Composer.json are bags. For a detailed description of the package, please check the official website http://v4.golaravel.com/docs/4.2/packages. Create a simple package with the following:
1. Environment configuration
We create a package with Artisan workbench
command, first to configure the app/confg/workbench.php
1 <? PHP 2 3 return Array (45 ' name ' = ' Brudeke ', 67 ' email ' = ' [ Email protected] ',89 );
2. Create the Package
Where vendor is the name of the vendor and is used to differentiate the same name of the package;
1 PHP Artisan Workbench Vendor/package--resources
Executing the above command will cause the Workbench directory to appear in the root directory, where workbench/vendor/package/src/vendor/ The packageserviceprovider.php is automatically generated under the package directory (hereinafter referred to as the Boot Class), which is equivalent to the boot class of the packet.
3. Adding a feature class to a package
Here we add a tool class in the package, creating utils/turbo.php in the same class as the "bootstrap"
1<?PHP2 3 namespace Vendor\package\utils;4 5 UseIlluminate\http\redirectresponse;6 7 classTurbo {8 9 Public function__construct () {}Ten One Public functionsetUp () { A Echo"This is a tool"; - } -}
4. Create facades
Facades function is to beautify the class, you can make the member function as a static member of the call, in the boot class peer creation facades/turbo.php
1<?PHP2 namespace Vendor\package\facades;3 4 UseIlluminate\support\facades\facade;5 6 classTurboextendsFacade {7 8 protected Static functiongetfacadeaccessor () {9 return' Turbo ';Ten } One}
5. Binding function class to IOC container
Modify the boot class as follows, where $defer is a deferred load service provider.
1<?PHP2 namespace Vendor\package;3 4 UseIlluminate\support\serviceprovider;5 6 classPackageserviceproviderextendsserviceprovider {7 8 /**9 * Indicates if loading of the provider is deferred.Ten * One * @var BOOL A */ - protected $defer=true; - the /** - * Bootstrap the application events. - * - * @return void + */ - Public functionboot () { + $this->package (' Vendor/package '); A } at - /** - * Register the service provider. - * - * @return void - */ in Public functionRegister () { - $this->app->bindshared (' Turbo ',function(){ to return New\vendor\package\utils\turbo; + }); - } the * /** $ * Get The services provided by the provider.Panax Notoginseng * - * @return Array the */ + Public functionprovides () A { the return Array(' Turbo '); + } - $}
6. Modify the configuration app/config/app.php
Add the following code to the providers array:
1 Array (2 ' Vendor\package\packageserviceprovider ', 3 ),
To add an alias to the aliases array Turbo:
1 Array (2 ' Turbo ' = ' Vendor\package\facades\turbo ',3 ),
The package can then be applied in the form of Turbo::setup () anywhere in the project.
Laravel Expansion Pack Development