Home > php教程 > PHP开发 > body text

Detailed explanation of how to create service provider and facade in laravel5

高洛峰
Release: 2016-12-23 17:24:17
Original
1538 people have browsed it

The example in this article describes how to create service provider and facade in laravel5. Share it with everyone for your reference, the details are as follows:

laravel5 creates a facade, which can register a service as a facade, so that you don’t need to bother using it. The article uses an example to illustrate how to create service provider and facade.我 Target

I hope I can create a Facade of AjaxResponse so that I can use it directly in Controller:

class MechanicController extends Controller {
  public function getIndex()
  {
    \AjaxResponse::success();
  }
}
Copy after login
E

its role is to regulate the format returned as

{
  code: "0"
  result: {
  }
}
Copy after login
E

Create

Creation

Service class

Create a class

<?php namespace App\Services;
class AjaxResponse {
  protected function ajaxResponse($code, $message, $data = null)
  {
    $out = [
      &#39;code&#39; => $code,
      &#39;message&#39; => $message,
    ];
    if ($data !== null) {
      $out[&#39;result&#39;] = $data;
    }
    return response()->json($out);
  }
  public function success($data = null)
  {
    $code = ResultCode::Success;
    return $this->ajaxResponse(0, &#39;&#39;, $data);
  }
  public function fail($message, $extra = [])
  {
    return $this->ajaxResponse(1, $message, $extra);
  }
}
Copy after login

in the app/Services folder

This AjaxResponse is a specific implementation class. Next we have to make a provider for this class

Create a provider

in the app/Providers folder Create class

<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AjaxResponseServiceProvider extends ServiceProvider {
  public function register()
  {
    $this->app->singleton(&#39;AjaxResponseService&#39;, function () {
      return new \App\Services\AjaxResponse();
    });
  }
}
Copy after login

Here we defined the Service name as AjaxResponseService when registering

Next we define a facade

Create facade

Create class

<?php namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class AjaxResponseFacade extends Facade {
  protected static function getFacadeAccessor() { return &#39;AjaxResponseService&#39;; }
}
Copy after login

in the app/Facades folder

Modify the configuration file

Okay, now we only need to mount these two things in app.php

<?php
return [
  ...
  &#39;providers&#39; => [
    ...
    &#39;App\Providers\RouteServiceProvider&#39;,
    &#39;App\Providers\AjaxResponseServiceProvider&#39;,
  ],
  &#39;aliases&#39; => [
    ...
    &#39;Validator&#39; => &#39;Illuminate\Support\Facades\Validator&#39;,
    &#39;View&#39;   => &#39;Illuminate\Support\Facades\View&#39;,
    &#39;AjaxResponse&#39; => &#39;App\Facades\AjaxResponseFacade&#39;,
  ],
];
Copy after login

Summary

It is relatively easy to use the facade in laravel5, Basically no difference from 4.

🎜I hope this article will be helpful to everyone’s PHP program design based on the Laravel framework. 🎜🎜For more detailed explanations of how to create service providers and facades in laravel5, please pay attention to the PHP Chinese website! 🎜
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template