Home >Backend Development >PHP Tutorial >Detailed explanation of the steps to register Facades in Laravel, laravelfacades_PHP tutorial

Detailed explanation of the steps to register Facades in Laravel, laravelfacades_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:48862browse

Detailed explanation of the steps to register Facades in Laravel, laravelfacades

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.

<&#63;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.

<&#63;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:

<&#63;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.

Articles you may be interested in:

  • PHP's Laravel framework combined with MySQL and Redis database deployment
  • Using message queue queue and asynchronous queue in PHP's Laravel framework Method
  • Laravel executes the migrate command prompt: No such file or directory solution
  • Detailed explanation of usage examples of Trait in Laravel
  • Laravel method to implement automatic dependency injection of constructors
  • How Laravel uses Caching to cache data to reduce database query pressure
  • Making an APP interface (API) based on laravel
  • Detailed explanation of the use of Eloquent object-relational mapping in PHP's Laravel framework
  • A summary of Laravel framework database CURD operations and coherent operations
  • In-depth analysis of event operations in PHP's Laravel framework

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1111347.htmlTechArticleDetailed explanation of the steps to register Facades in Laravel, laravelfacades This article describes the steps to register Facades in Laravel. Share it with everyone for your reference, the details are as follows: Register the class in Laravel as...
Statement:
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