Laravel5 How to create service provider and facade _php instance

Source: Internet
Author: User
Tags smarty template

The example in this article describes how Laravel5 creates service provider and facades. Share to everyone for your reference, specific as follows:

Laravel5 creates a façade that allows a service to be registered as a façade so that you don't need to use it in trouble. The article uses an example to illustrate how to create a service provider and a façade.

Goal

I want me to create a ajaxresponse façade that can be used directly in controller:

Class Mechaniccontroller extends Controller {public
  function GetIndex ()
  {
    \ajaxresponse::success ();
  }
}

Its function is that the canonical return format is

{
  code: ' 0 ' result
  : {
  }
}

Steps

Create service class

To create a class in the App/services folder

<?php namespace App\services;
Class Ajaxresponse {
  protected function ajaxresponse ($code, $message, $data = null)
  {
    $out = [
      ' Code ' =&G T $code,
      ' message ' => $message,
    ];
    if ($data!== null) {
      $out [' result '] = $data;
    }
    return response ()->json ($out);
  Public Function success ($data = null)
  {
    $code = resultcode::success;
    return $this->ajaxresponse (0, ', $data);
  }
  Public function fail ($message, $extra = [])
  {return
    $this->ajaxresponse (1, $message, $extra);
  }


This ajaxresponse is a concrete implementation class, and we're going to do a provider for this class

Create Provider

To create a class in the App/providers folder

<?php namespace App\providers;
Use Illuminate\support\serviceprovider;
Class Ajaxresponseserviceprovider extends ServiceProvider {public
  function register ()
  {
    $this->app- >singleton (' Ajaxresponseservice ', function () {return
      new \app\services\ajaxresponse ();}
    );
  }


Here we define the service name in the register as Ajaxresponseservice.

Now we're going to define a backyards façade.

Create a façade

To create a class in the App/facades folder

<?php namespace App\facades;
Use Illuminate\support\facades\facade;
Class Ajaxresponsefacade extends Facade {
  protected static function Getfacadeaccessor () {return ' Ajaxresponseservice '; }
}

Modify configuration file

All right, now we just need to go to the app.php and mount these two things.

<?php return
[
  ...
  ] Providers ' => [
    ...
    ] App\providers\routeserviceprovider ',
    ' App\providers\ajaxresponseserviceprovider ',
  ],
  ' aliases ' = > [
    ...
    ] Validator ' => ' illuminate\support\facades\validator ',
    ' View '   => ' Illuminate\support\facades\view ' ,
    ' ajaxresponse ' => ' App\facades\ajaxresponsefacade ',
  ],
];

Summarize

It's easier to use a façade in the Laravel5, basically and 4.

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.