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.