How to create custom Facade in Laravel 4

后端 未结 2 1758
一生所求
一生所求 2021-01-02 22:46

Looked up a few tutorials on facades and laravel 4... tried some... not liked the way they work.

For instance, they don\'t all provide a way of defining where to sto

2条回答
  •  無奈伤痛
    2021-01-02 22:57

    There are three components to making a Facade:

    • The wanna be Facade Class, that class that needs to become a facade.
    • The Facade required Class, which tells Laravel which registered class it pertains to
    • A Service Provider, which registers the Facade class in the App container

    1. the wanna be Facade Class:

     $email,
                'password' => $password,
            ]);
        }
    
        /**
         * @return mixed
         */
        public function logout()
        {
            return Sentry::logout();
        }
    
    }
    

    2. the required class for the facade to work:

    note: authentication_service can be anything you want (its the name of the component registered in the IOC)

    3. the service provider

    app['authentication_service'] = $this->app->share(function($app)
            {
                return $app->make('Moubarmij\Services\ModelsServices\AuthenticationService');
            });
    
            // Shortcut to auto add the Alias in app/config/app.php
            $this->app->booting(function()
            {
                $loader = \Illuminate\Foundation\AliasLoader::getInstance();
                $loader->alias('AuthenticationService', 'Moubarmij\Facades\AuthenticationServiceFacade');
            });
    
        }
    }
    

提交回复
热议问题