Home > PHP Framework > Laravel > body text

How to add custom facade and service provider using laravel

little bottle
Release: 2019-04-29 10:37:17
forward
3508 people have browsed it

When using PHP’s laravel framework for project development, we often use the facade and Service provider, let's explore how to write your own facade and service provider (the following code is based on laravel 5.2*).

1. Create a custom class

Create a utils\ToolBar.php file under the app directory. This is our tool class, which contains the code we defined. .

<?phpnamespace App\Utils;class ToolBar{
   public function get()
   {
       return &#39;Hello my facade&#39;;
   }}
Copy after login

2. Create a service provider

Execute the command in the root directory of the project: php artisan make:provider ToolServiceProvider to create a service provider and add what we just The tool class written is registered in the container.

<?phpnamespace App\Providers;use Illuminate\Support\ServiceProvider;use App\Utils\ToolBar;class ToolServiceProvider extends ServiceProvider{
   /**
    * Bootstrap the application services.
    *
    * @return void
    */
   public function boot()
   {
       //
   }

   /**
    * Register the application services.
    *
    * @return void
    */
   public function register()
   {
       $this->app->bind(&#39;tool&#39;,function(){
           return new ToolBar();
       });
   }}
Copy after login

3. Group registration service provider

Register the service provider we just added in the providers attribute in the config\app.php file, that is, add:

App\Providers\ToolServiceProvider::class,
Copy after login

4. Create a facade class

Create a facade class under the app directory, App\Facades\Tool.php, the directory here is created by myself, this You can create whatever you want. As long as it is consistent when registering later, it will be fine. The code is as follows:

<?phpnamespace App\Facades;use Illuminate\Support\Facades\Facade;class Tool extends Facade{
   protected static function getFacadeAccessor()
   {
       return &#39;tool&#39;;
   }}
Copy after login

5. Group registration facade class

Add the following code to the aliases attribute of config\app.php:

&#39;Tool&#39; => App\Facades\Tool::class,
Copy after login

After completing the above steps, we can call to test whether the created facade and service provider are valid
Add the following code in routes\console.php:

Artisan::command(&#39;testFacade&#39;,function(){
    dd(tool::get());});
Copy after login

Then in the terminal of the project root directory, call The following command:

php artisan testFacade
Copy after login

If Hello my facade is output, it means the registration is successful. Next, we can use the custom facade anywhere in the project.

When using PHP's laravel framework for project development, we often use the facade and service providers that come with the laravel framework. , let's explore how to write our own facade and service provider (the following code is based on laravel 5.2*).

Related tutorials: laravel video tutorial

The above is the detailed content of How to add custom facade and service provider using laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!