<p>Service provider içinde bağlanabilir:</p>

<div class="highlight js-code-highlight">
    <pre class="highlight php"><code>$this->app->singleton(SmsConfig::class, function() {
return new SmsConfig(
    apiKey: config('sms.api_key'),
    apiSecret: config('sms.api_secret'),
    sender: config('sms.sender'),
    timeout: config('sms.timeout'),
);

});

<p>Sonra servis içinde:</p>

<div class="highlight js-code-highlight">
    <pre class="highlight php"><code>final readonly class SmsService {
public function __construct(
    private SmsConfig $config,
) {}

public function send(string $phone, string $message): void {
    // $this->config->apiKey
}

}

<p>Bu Laravel’in zorunlu standardı değildir. Ancak kritik ve büyüyen entegrasyonlarda tip güvenliği, IDE desteği ve test edilebilirlik açısından temiz bir mimari tercih olabilir.</p>

<hr/>

<blockquote>
    <p><code>env()</code> config dosyalarında, <code>config()</code> uygulama kodunda kullanılır.</p>
</blockquote>

<p>Bu basit alışkanlık, localde çalışan ama production’da sorun çıkaran birçok hatanın önüne geçer.</p>

Kaynak: Orijinal Makale

Bu Makaleyi Paylaş