創建
php artisan make:provider NoticationServiceProvider
註冊服務提供者
將該類追加到配置文件config/app.php的providers數組中
'providers' => [
//其他服務提供者
App\Providers\TestServiceProvider::class,
],
兩種方式
第一,通過綁定服務ID的方式定義的服務,只能通過服務ID來獲取
// 定義服務
class NotificationServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->singleton('service_id', function () {
return new Service();
})
}
}
// 獲取服務,以下方式等價 controller
public function store()
{
app('service_id')
}
第二,你想通過接口的類型提示(Type Hint)來自動解析並注入服務
public function register()
{
// 綁定接口到實例
$this->app->bind('Namespace\To\Your\Interface\Pay', 'Namespace\To\Your\Class\Weixin');
}
第三,像上邊這樣,通過綁定接口到實例,只能自動解析爲一個實例,也就是你綁定的實例;如果你想要的結果是,不同的類,構造器通過類型提示(Type Hint)相同的接口,注入不同的實例,可以像下邊這樣(上下文綁定,Context Binding)
public function register()
{
$this->app->when('Namespace\To\Your\Class\A')
->needs('Namespace\To\Your\Interface\Pay')
->give('Namespace\To\Your\Class\Weixin');
$this->app->when('Namespace\To\Your\Class\B')
->needs('Namespace\To\Your\Interface\Pay')
->give('Namespace\To\Your\Class\Ali');
}
通過上下文綁定,A的實例會注入Weixin的實例,而B的實例會注入Ali的實例。像下邊:
class A
{
public functions __construct(Pay $pay)
{
var_dump($pay instanceof Weixin); // True
}
}
class B
{
public functions __construct(Pay $pay)
{
var_dump($pay instanceof Ali); // True
}
}
----------------------------------------------------------------------------------------------------------
1.綁定一個class,調用的時候直接實例化這個class
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class NotificationServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind('notification.forUser', 'App\Library\Services\NotificationForUser');
}
}
NotificationServiceInterface.php
namespace App\Library\Services\Contracts;
Interface NotificationServiceInterface
{
public function printNotice();
}
Notification.php
namespace App\Library\Services;
use App\Library\Services\Contracts\NotificationServiceInterface;
class NotificationForUser implements NotificationServiceInterface
{
public function printNotice()
{
return 'Welcome ' . auth()->user()->name . ' !';
}
}
public function store()
{
if (!auth()->attempt(request(['name', 'password']))) {
return back()->withErrors([
'message' => "Please check your credientials and try again."
]);
}
// Get notice from session('notice') in template.
return redirect()->home()->with('notice' , app('notification.forUser')->printNotice());
}
2.綁定一個接口,調用的時候直接實例化這個接口
需要在調用的類中注入接口才會生效
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class NotificationServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\Library\Services\Contracts\NotificationServiceInterface',
function ($app) {
return new \App\Library\Services\NotificationForUser();
});
}
}
use App\Library\Services\Contracts\NotificationServiceInterface;
...
public function store(NotificationServiceInterface $NotificationForUserInstance)
{
if (!auth()->attempt(request(['name', 'password']))) {
return back()->withErrors([
'message' => "Please check your credientials and try again."
]);
}
// Get notice from session('notice') in template.
return redirect()->home()->with('notice' , $NotificationForUserInstance->printNotice());
}