Home >Backend Development >PHP Tutorial >Detailed explanation of the steps to register Facades in Laravel, laravelfacades_PHP tutorial
This article describes the steps to register Facades in Laravel. Share it with everyone for your reference, the details are as follows:
To register a class as Fcade in Laravel, you can use the Ioc container. Each time you use this class, the class will only be initialized once, similar to the singleton mode, and you can call the class method like a static method. The following is in Laravel Steps to register Facades.
1. Add a new method to the register method in Providers/AppServiceProvider.php in the project app directory. The code is as follows.
/** * Register any application services. * * @return void */ public function register() { $this->registerTestModel(); } private function registerTestModel() { $this->app->singleton('testmodel', function ($app) { $model = 'App\Models\Test'; return new $model(); }); $this->app->alias('testmodel', 'App\Models\Test'); }
Here, register the Test class whose namespace is AppModels as a singleton mode, and give it an alias testmodel. The file location of this Test class is app/Models/Test.php.
2. Create a Facade class
Add a new file in the appFacades directory of the project root directory, such as Test.php. The code is as follows. If the directory does not exist, you can create a new one.
<?php namespace App\Facades; use Illuminate\Support\Facades\Facade; class Test extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'testmodel'; } }
By inheriting Facade and overloading the getFacadeAccessor method, return the alias of the previously bound singleton mode class.
3. Use Facade
After going through the previous steps, you can use the Test Facade. The following example is how to use the Facade in the controller.
<?php namespace App\Http\Controllers; use App\Facades\Test; use Illuminate\Routing\Controller; class TestController extends Controller { public function __construct() { Test::show(); Test::show(); } }
First look at the content of this original class Test.php:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Test extends Model { protected $table = 'tt'; public static $times = 0; public function __construct() { self::$times++; parent::__construct(); } public function show() { echo self::$times . '<br>'; } }
After registering the Facade, calling the show method is in the form of Test::show(), and similar to the singleton mode, it will not be instantiated multiple times, and the call is also very simple.
PS: The above are only the methods and steps for registering Facade. In actual projects, the Model layer may need to be further encapsulated.
Reprinted from: Xiaotan Blog http://www.tantengvip.com/2016/01/laravel-facades-register/
Readers who are interested in more information about Laravel can check out the special topics on this site: "Introduction and Advanced Tutorial on Laravel Framework", "Summary of PHP Excellent Development Framework", "Basic Tutorial on Getting Started with Smarty Templates", "php Date and Time" Usage Summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the Laravel framework.