Steps to register facades in Laravel _php instance

Source: Internet
Author: User
Tags smarty template

This article describes the steps for registering facades in laravel. Share to everyone for your reference, specific as follows:

Registering a class as a fcade in Laravel can use the IOC container, which initializes the class only once per use of the class, similar to a single case pattern, and can invoke the method of a class using static methods, and the following steps for registering facades in laravel.

1. The new method of the Register method in the providers/appserviceprovider.php of 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 the namespace is app\models the test class is registered as a single case pattern, and the individual name Testmodel. The file location of this test class is app/models/test.php.

2. Create a façade class

In the project root directory app\facades new files, such as test.php, code as follows, 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 the façade, overload the Getfacadeaccessor method, returning the alias of the class of the previously bound single case pattern.

3. Using a façade

After the previous steps, you can use the façade of test, as the following example is the way to use a façade in a 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 ();
 }


Let's take a look at the 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 façade, 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: The above only for the registration of the façade of methods and procedures, the actual project may also need to further encapsulate the model layer.

Turn from: Small Talk blog http://www.tantengvip.com/2016/01/laravel-facades-register/

More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "

I hope this article will help you with the PHP program design based on Laravel framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.