佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Laravel如何創(chuàng)建服務(wù)器提供者實例代碼

Laravel如何創(chuàng)建服務(wù)器提供者實例代碼

熱門標簽:阿里云 電子圍欄 服務(wù)器配置 科大訊飛語音識別系統(tǒng) 團購網(wǎng)站 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù)

前言

Laravel服務(wù)器容器:是用于管理類依賴和執(zhí)行依賴注入的工具。下面我們演示下如何創(chuàng)建服務(wù)器提供者,它是Laravel的核心。話不多說了,來一起看看詳細的介紹吧

在app/Contracts目錄下創(chuàng)建TestContract.php文件,其內(nèi)容為:

?php 
namespace App\Contracts; 

interface TestContract { 
 public function callMe($controller); 
}

在app/Services目錄下創(chuàng)建TestService.php文件,其內(nèi)容為:

?php 
namespace App\Services; 
use App\Contracts\TestContract; 

class TestService implements TestContract { 
 public function callMe($controller){ 
 dd("Call me from TestServiceProvider in ".$controller); 
 } 
}

在config/app.php文件中providers中添加內(nèi)容,以便進行注冊:

... 
App\Providers\RiakServiceProvider::class,

創(chuàng)建1個服務(wù)提供類:

php artisan make:provider RiakServiceProvider 

其內(nèi)容為:

?php 

namespace App\Providers; 

use App\Services\TestService; 
use Illuminate\Support\ServiceProvider; 

class RiakServiceProvider extends ServiceProvider 
{ 
 /** 
 * Bootstrap the application services. 
 * 
 * @return void 
 */ 
 public function boot() 
 { 
 // 
 } 

 /** 
 * Register the application services. 
 * 
 * @return void 
 */ 
 public function register() 
 { 
 $this->app->bind("App\Contracts\TestContract",function(){ 
  return new TestService(); 
 }); 
 } 
}

在ServiceProvider中提供了2個方法,其中register方法用于注冊服務(wù),而boot用于引導(dǎo)服務(wù)。

在控制器IndxController中添加如下內(nèi)容:

?php 

namespace App\Http\Controllers; 

use App; 
use Illuminate\Http\Request; 
use App\Contracts\TestContract; 

class IndexController extends Controller 
{ 
 public function __construct(TestContract $test){ 
 $this->test = $test; 
 } 
 public function index(){ 
 $this->test->callMe("IndexController"); 
 } 
}

訪問瀏覽器可以得到如下的結(jié)果:

"Call me from TestServiceProvider in IndexController" 

另外,還可以使用App的make方法進行調(diào)用。

public function index(){ 
 $test = App::make('test'); 
 $test->callMe('IndexController'); 
 }

其結(jié)果也是一樣的。

參考文章:

  • https://laravelacademy.org/post/796.html
  • https://laravelacademy.org/post/93.html

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • Laravel框架實現(xiàn)的rbac權(quán)限管理操作示例
  • Laravel5權(quán)限管理方法詳解
  • Laravel 5 框架入門(二)構(gòu)建 Pages 的管理功能
  • Laravel5.0+框架郵件發(fā)送功能實現(xiàn)方法圖文與實例詳解
  • Laravel框架集成UEditor編輯器的方法圖文與實例詳解
  • Laravel框架自定義驗證過程實例分析
  • laravel5.3 vue 實現(xiàn)收藏夾功能實例詳解
  • Laravel接收前端ajax傳來的數(shù)據(jù)的實例代碼
  • 在Laravel框架里實現(xiàn)發(fā)送郵件實例(郵箱驗證)
  • Laravel路由設(shè)定和子路由設(shè)定實例分析
  • Laravel框架實現(xiàn)簡單的學(xué)生信息管理平臺案例【附源碼下載】

標簽:衢州 江蘇 蚌埠 衡水 大理 棗莊 廣元 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Laravel如何創(chuàng)建服務(wù)器提供者實例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    福贡县| 大渡口区| 昌邑市| 牙克石市| 酒泉市| 嵩明县| 广元市| 昌平区| 东阿县| 改则县| 石泉县| 卫辉市| 永嘉县| 松溪县| 合川市| 宁南县| 定边县| 年辖:市辖区| 丰镇市| 铜陵市| 安阳县| 电白县| 泽库县| 石景山区| 孟津县| 库尔勒市| 桃江县| 麦盖提县| 翁牛特旗| 兴业县| 靖远县| 玛沁县| 沿河| 平原县| 永善县| 昌图县| 荔浦县| 定日县| 盐城市| 通化市| 厦门市|