Laravel如何創(chuàng)建服務(wù)器提供者實(shí)例代碼
前言
Laravel服務(wù)器容器:是用于管理類依賴和執(zhí)行依賴注入的工具。下面我們演示下如何創(chuàng)建服務(wù)器提供者,它是Laravel的核心。話不多說了,來一起看看詳細(xì)的介紹吧
在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)容,以便進(jìn)行注冊:
... 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方法進(jìn)行調(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框架實(shí)現(xiàn)的rbac權(quán)限管理操作示例
- Laravel5權(quán)限管理方法詳解
- Laravel 5 框架入門(二)構(gòu)建 Pages 的管理功能
- Laravel5.0+框架郵件發(fā)送功能實(shí)現(xiàn)方法圖文與實(shí)例詳解
- Laravel框架集成UEditor編輯器的方法圖文與實(shí)例詳解
- Laravel框架自定義驗(yàn)證過程實(shí)例分析
- laravel5.3 vue 實(shí)現(xiàn)收藏夾功能實(shí)例詳解
- Laravel接收前端ajax傳來的數(shù)據(jù)的實(shí)例代碼
- 在Laravel框架里實(shí)現(xiàn)發(fā)送郵件實(shí)例(郵箱驗(yàn)證)
- Laravel路由設(shè)定和子路由設(shè)定實(shí)例分析
- Laravel框架實(shí)現(xiàn)簡單的學(xué)生信息管理平臺案例【附源碼下載】
相關(guān)文章
詳解PHP中strlen和mb_strlen函數(shù)的區(qū)別
在PHP中,strlen與mb_strlen是求字符串長度的函數(shù),但是對于一些初學(xué)者來說,如果不看手冊,也許不太清楚其中的區(qū)別2014-03-03
解決PHP使用CURL發(fā)送GET請求時傳遞參數(shù)的問題
今天小編就為大家分享一篇解決PHP使用CURL發(fā)送GET請求時傳遞參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Zend Framework教程之Zend_Config_Xml用法分析
這篇文章主要介紹了Zend Framework教程之Zend_Config_Xml用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Zend_Config_Xml的功能,使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03
thinkphp,onethink和thinkox中驗(yàn)證碼不顯示的解決方法分析
這篇文章主要介紹了thinkphp,onethink和thinkox中驗(yàn)證碼不顯示的解決方法,簡單分析了thinkPHP驗(yàn)證碼不顯示的原因與相應(yīng)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
CodeIgniter配置之a(chǎn)utoload.php自動加載用法分析
這篇文章主要介紹了CodeIgniter配置之a(chǎn)utoload.php自動加載用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了CodeIgniter自動加載機(jī)制的原理與使用方法,需要的朋友可以參考下2016-01-01

