서비스 컨테이너(IoC Inversion of Control)는 객체의 생성 방법을 알고 있으며, 이를 대신해주고 필요한 곳에 주입해준다.
ServiceProvider의 bind() 을 통해서 미리 설정된 객체를 연결시켜 둔다.
어떤 객체를 컨테이너에 요구하면 객체를 적절하게 생성해서 넘겨준다.
라라벨에서는 기본적으로 해결해주는 것도 있고 의존성 해결을 위해 바인딩을 거쳐야 하는 일도 있다.
public function __invoke(Request $request)
{
return view('welcome');
}
public function __invoke()
{
// $request = app(Request::class);
$request = app->make(Request::class);
return get_class($request); // Illuminate\Http\Request
}
서비스 컨테이너가 객체 생성을 모른다면?
-> app->make(Request::class);
타입힌트를 했는데, 구체 클래스를 원한다면?
-> 서비스 프로바이더의 register() 함수에서 $this->app->bind() 함수를 이용한다.
서비스 컨테이너는 서비스 프로바이더에서 설정하는 class 를 참조해서 객체를 생성한다.
config/app.php 에는 기본으로 참조해야할 서비스가 등록되어 있다.
\Illuminate\Auth\AuthServiceProvider::class,
\Illuminate\Broadcasting\BroadcastServiceProvider::class,
\Illuminate\Bus\BusServiceProvider::class,
\Illuminate\Cache\CacheServiceProvider::class,
\Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
\Illuminate\Cookie\CookieServiceProvider::class,
\Illuminate\Database\DatabaseServiceProvider::class,
\Illuminate\Encryption\EncryptionServiceProvider::class,
\Illuminate\Filesystem\FilesystemServiceProvider::class,
\Illuminate\Foundation\Providers\FoundationServiceProvider::class,
\Illuminate\Hashing\HashServiceProvider::class,
\Illuminate\Mail\MailServiceProvider::class,
\Illuminate\Notifications\NotificationServiceProvider::class,
\Illuminate\Pagination\PaginationServiceProvider::class,
\Illuminate\Pipeline\PipelineServiceProvider::class,
\Illuminate\Queue\QueueServiceProvider::class,
\Illuminate\Redis\RedisServiceProvider::class,
\Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
\Illuminate\Session\SessionServiceProvider::class,
\Illuminate\Translation\TranslationServiceProvider::class,
\Illuminate\Validation\ValidationServiceProvider::class,
\Illuminate\View\ViewServiceProvider::class,
여기에 사용자 별도의 서비스를 등록해서 사용하는데 Laravel 에서는 친절하게 미리 만들어둔 서비스가 있다.
app/Providers/AppServiceProvider.php
app/Providers/AuthServiceProvider.php
app/Providers/BroadcastServiceProvider.php
app/Providers/EventServiceProvider.php
app/Providers/RouteServiceProvider.php
config/app.php
'providers' => ServiceProvider::defaultProviders()->merge([
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
])->toArray(),
위 서비스는 ServiceProvider 를 상속한다.
상속하면 register(), boot() 메뉴를 구현해야 한다.
register() 는 바이딩 시킬때
boot() 는 서비스가 로딩된 이후에 실행된다.
'Laravel' 카테고리의 다른 글
Laravel - API Resource 만들기 (0) | 2024.01.31 |
---|---|
Laravel - Enum 생성, 조회 및 Validation 처리하기 (사용자 권한 구분하기) (0) | 2024.01.29 |
Laravel - sanctum 에 대해서 (0) | 2024.01.09 |
Laravel - Event에 대해서 (0) | 2024.01.08 |
Laravel - FormRequest에 대해서 (0) | 2024.01.02 |
서비스 컨테이너(IoC Inversion of Control)는 객체의 생성 방법을 알고 있으며, 이를 대신해주고 필요한 곳에 주입해준다.
ServiceProvider의 bind() 을 통해서 미리 설정된 객체를 연결시켜 둔다.
어떤 객체를 컨테이너에 요구하면 객체를 적절하게 생성해서 넘겨준다.
라라벨에서는 기본적으로 해결해주는 것도 있고 의존성 해결을 위해 바인딩을 거쳐야 하는 일도 있다.
public function __invoke(Request $request)
{
return view('welcome');
}
public function __invoke()
{
// $request = app(Request::class);
$request = app->make(Request::class);
return get_class($request); // Illuminate\Http\Request
}
서비스 컨테이너가 객체 생성을 모른다면?
-> app->make(Request::class);
타입힌트를 했는데, 구체 클래스를 원한다면?
-> 서비스 프로바이더의 register() 함수에서 $this->app->bind() 함수를 이용한다.
서비스 컨테이너는 서비스 프로바이더에서 설정하는 class 를 참조해서 객체를 생성한다.
config/app.php 에는 기본으로 참조해야할 서비스가 등록되어 있다.
\Illuminate\Auth\AuthServiceProvider::class,
\Illuminate\Broadcasting\BroadcastServiceProvider::class,
\Illuminate\Bus\BusServiceProvider::class,
\Illuminate\Cache\CacheServiceProvider::class,
\Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
\Illuminate\Cookie\CookieServiceProvider::class,
\Illuminate\Database\DatabaseServiceProvider::class,
\Illuminate\Encryption\EncryptionServiceProvider::class,
\Illuminate\Filesystem\FilesystemServiceProvider::class,
\Illuminate\Foundation\Providers\FoundationServiceProvider::class,
\Illuminate\Hashing\HashServiceProvider::class,
\Illuminate\Mail\MailServiceProvider::class,
\Illuminate\Notifications\NotificationServiceProvider::class,
\Illuminate\Pagination\PaginationServiceProvider::class,
\Illuminate\Pipeline\PipelineServiceProvider::class,
\Illuminate\Queue\QueueServiceProvider::class,
\Illuminate\Redis\RedisServiceProvider::class,
\Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
\Illuminate\Session\SessionServiceProvider::class,
\Illuminate\Translation\TranslationServiceProvider::class,
\Illuminate\Validation\ValidationServiceProvider::class,
\Illuminate\View\ViewServiceProvider::class,
여기에 사용자 별도의 서비스를 등록해서 사용하는데 Laravel 에서는 친절하게 미리 만들어둔 서비스가 있다.
app/Providers/AppServiceProvider.php
app/Providers/AuthServiceProvider.php
app/Providers/BroadcastServiceProvider.php
app/Providers/EventServiceProvider.php
app/Providers/RouteServiceProvider.php
config/app.php
'providers' => ServiceProvider::defaultProviders()->merge([
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
])->toArray(),
위 서비스는 ServiceProvider 를 상속한다.
상속하면 register(), boot() 메뉴를 구현해야 한다.
register() 는 바이딩 시킬때
boot() 는 서비스가 로딩된 이후에 실행된다.
'Laravel' 카테고리의 다른 글
Laravel - API Resource 만들기 (0) | 2024.01.31 |
---|---|
Laravel - Enum 생성, 조회 및 Validation 처리하기 (사용자 권한 구분하기) (0) | 2024.01.29 |
Laravel - sanctum 에 대해서 (0) | 2024.01.09 |
Laravel - Event에 대해서 (0) | 2024.01.08 |
Laravel - FormRequest에 대해서 (0) | 2024.01.02 |