The steps to register facades in Laravel are detailed, laravelfacades
This article describes the steps for registering facades in laravel. Share to everyone for your reference, as follows:
Registering a class as Fcade in Laravel can use the IOC container, which initializes the class once each time it is used, like a singleton pattern, and can invoke a method of the class like a static method, following the steps to register facades in Laravel.
1. New method for register method in providers/appserviceprovider.php of Project app directory, code below.
/** * 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 ');}
This is to register the namespace as App\models's Test class as a singleton pattern, and take the individual name Testmodel. The file location of the test class is app/models/test.php.
2. Create a Facade class
In the project root directory app\facades directory new files, such as test.php, code as follows, directory does not exist can create a new one.
<?phpnamespace 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, the Getfacadeaccessor method is overloaded, returning the alias of the class of the previously bound singleton pattern.
3. Using facade
After the previous steps, you can use the test facade, the following example is the way to use facade in the controller.
<?phpnamespace 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 contents of this primitive class test.php:
<?phpnamespace 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. '
'; }}
After registering facade, calling the Show method is the form of test::show (), and similar singleton patterns are not instantiated more than once, and the invocation is simple.
PS: Above is only the method and procedure of registering facade, the model layer may need further encapsulation in actual project.
Turn from: Small Talk blog http://www.tantengvip.com/2016/01/laravel-facades-register/
More interested in laravel related content readers can view this site topic: "Laravel Framework Introductory and Advanced tutorial", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming introduction tutorial "," PHP String Usage Summary "," Introduction to Php+mysql Database Operations "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on Laravel framework.
Articles you may be interested in:
- PHP's Laravel framework combined with MySQL and Redis database deployment
- Methods for using Message Queuing queue and asynchronous queues in PHP's Laravel framework
- Laravel Execute migrate command prompt: No such solution for file or directory
- A detailed example of trait usage in Laravel
- Laravel method of implementing automatic dependency injection of constructors
- Laravel ways to reduce database query pressure using caching cache data
- Create an App interface (API) based on Laravel
- Eloquent Object relational mapping in PHP's Laravel framework using
- Laravel Framework Database curd operation and coherent operation summary
- In-depth parsing of event events operations in PHP's Laravel framework
http://www.bkjia.com/PHPjc/1111347.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111347.html techarticle The steps to register facades in Laravel are detailed, laravelfacades this article describes the steps to register facades in Laravel. Share to everyone for your reference, as follows: Register the class as ... in Laravel .