laravel5創(chuàng)建service provider和facade的方法詳解
本文實(shí)例講述了laravel5創(chuàng)建service provider和facade的方法。分享給大家供大家參考,具體如下:
laravel5創(chuàng)建一個(gè)facade,可以將某個(gè)service注冊(cè)個(gè)門面,這樣,使用的時(shí)候就不需要麻煩地use 了。文章用一個(gè)例子說(shuō)明怎么創(chuàng)建service provider和 facade。
目標(biāo)
我希望我創(chuàng)建一個(gè)AjaxResponse的facade,這樣能直接在controller中這樣使用:
class MechanicController extends Controller {
public function getIndex()
{
\AjaxResponse::success();
}
}
它的作用就是規(guī)范返回的格式為
{
code: "0"
result: {
}
}
步驟
創(chuàng)建Service類
在app/Services文件夾中創(chuàng)建類
<?php namespace App\Services;
class AjaxResponse {
protected function ajaxResponse($code, $message, $data = null)
{
$out = [
'code' => $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);
}
}
這個(gè)AjaxResponse是具體的實(shí)現(xiàn)類,下面我們要為這個(gè)類做一個(gè)provider
創(chuàng)建provider
在app/Providers文件夾中創(chuàng)建類
<?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();
});
}
}
這里我們?cè)趓egister的時(shí)候定義了這個(gè)Service名字為AjaxResponseService
下面我們?cè)俣x一個(gè)門臉facade
創(chuàng)建facade
在app/Facades文件夾中創(chuàng)建類
<?php namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class AjaxResponseFacade extends Facade {
protected static function getFacadeAccessor() { return 'AjaxResponseService'; }
}
修改配置文件
好了,下面我們只需要到app.php中掛載上這兩個(gè)東東就可以了
<?php
return [
...
'providers' => [
...
'App\Providers\RouteServiceProvider',
'App\Providers\AjaxResponseServiceProvider',
],
'aliases' => [
...
'Validator' => 'Illuminate\Support\Facades\Validator',
'View' => 'Illuminate\Support\Facades\View',
'AjaxResponse' => 'App\Facades\AjaxResponseFacade',
],
];
總結(jié)
laravel5中使用facade還是較為容易的,基本和4沒(méi)啥區(qū)別。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
ThinkPHP模板中數(shù)組循環(huán)實(shí)例
這篇文章主要介紹了ThinkPHP模板中數(shù)組循環(huán),以實(shí)例形式展示了ThinkPHP采用foreach標(biāo)簽循環(huán)輸出數(shù)組的方法,需要的朋友可以參考下2014-10-10
PHP實(shí)現(xiàn)AJAX動(dòng)態(tài)網(wǎng)頁(yè)及相關(guān)函數(shù)詳解
ajax其實(shí)是利用javascript向服務(wù)器請(qǐng)求數(shù)據(jù),然后局部修改頁(yè)面,下面這篇文章主要給大家介紹了關(guān)于PHP實(shí)現(xiàn)AJAX動(dòng)態(tài)網(wǎng)頁(yè)及相關(guān)函數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
PHP實(shí)現(xiàn)微信公眾平臺(tái)音樂(lè)點(diǎn)播
首先說(shuō)一下思路,微信提供了接口,只要數(shù)據(jù)格式滿足它所給的接口的XML格式即可以發(fā)送給關(guān)注者對(duì)應(yīng)的音樂(lè)2014-03-03
php微信開(kāi)發(fā)之帶參數(shù)二維碼的使用
這篇文章主要為大家詳細(xì)介紹了php微信開(kāi)發(fā)之帶參數(shù)二維碼的使用,感興趣的小伙伴們可以參考一下2016-08-08
ajax完美實(shí)現(xiàn)兩個(gè)網(wǎng)頁(yè) 分頁(yè)功能的實(shí)例代碼
ajax完美實(shí)現(xiàn)兩個(gè)網(wǎng)頁(yè) 分頁(yè)功能的實(shí)例代碼,需要的朋友可以參考一下2013-04-04

