Asked Jul 18th, 2018 9:12 a.m. 234 1 1
  • 234 1 1
0

Service Provider Laravel

Share
  • 234 1 1

các anh chị cho em hỏi giả sử trong file AppServiceProvider hàm register() em bind như sau

$this->app->bind(
        \App\Contracts\Repositories\PostRepository::class,
        \App\Repositories\PostRepositoryEloquent::class,
),

$this->app->bind(
        \App\Contracts\Repositories\PostRepository::class,
        \App\Repositories\NewRepositoryEloquent::class,
),

trong Controller khai báo

protected $post;

public function __construct(PostRepository $repository)
{
    $this->post = $repository;
}

thì nó hiểu implementation nào sẽ được sử dụng ạ ????

1 ANSWERS


Answered Jul 18th, 2018 9:45 a.m.
Accepted
+5

Em cứ tưởng tượng rằng mình có một mảng là $bindings = ['abstract' => 'instance'], mỗi lần em gọi hàm bind thì Laravel sẽ thực hiện việc update cái mảng $bindings đó.

Giống như khi update một mảng bình thường thôi, nếu em đưa vào một key đã tồn tại trong mảng, thì giá trị của nó sẽ được update. Hay hiểu đơn giản là cái sau sẽ ghi đè lên cái trước.

Vậy câu trả lời là Server Container sẽ (và chỉ có thể) resolve ra implementation được khai báo trong câu lệnh bind chạy sau cùng thôi 😄

Share
Viblo
Let's register a Viblo Account to get more interesting posts.