简体   繁体   中英

How to bind .env values in Laravel Service using Service Container and Service Provider

What I want to achieve is I have a service class name ' SmsService '

<?php
namespace App\Services;

use App\Contracts\SmsServiceContract;
use App\Models\Student;
use Twilio\Rest\Client;

    class SmsService implements SmsServiceContract{
    
            private $account_sid;
            private $account_token;
            private $account_from;
            private $reciever;
    
            public function __construct(){
                $this->account_sid = env("TWILIO_SID");
                $this->account_token = env("TWILIO_TOKEN");
                $this->account_from = env("TWILIO_FROM");
                $this->reciever = new Client($this->account_sid, $this->account_token);
            }
    
        public function sendSingleSms($phone_number, $message){
            
            $this->reciever->messages->create($phone_number,[
                'from' => $this->account_from,
                'body' => $message
            ]);
        }
    
    }

I'm binding this service in a service container like this.

$this->app->bind(SmsServiceContract::class, SmsService::class);

The issue is that I'm getting null when I'm trying to get TWILIO_SID from .env file. How could I get .env data in SmsService class?

 first time config/app.php edit add this line 'account_sid' => env('TWILIO_SID'), 'account_token' => env('TWILIO_TOKEN'), 'account_from' => env('TWILIO_FROM'), and change this line public function __contruct(){ $this->account_sid = config('app.account_sid'); $this->account_token = config('app.account_token'); $this->account_from = config('app.account_from'); $this->reciever = new Client($this->account_sid, $this->account_token); } because you are production mode runnig

To access keys from.env file, best way is to access them through congif/app.php file. Add these lines in Config/app.php

 'Account_SID' => env('TWILIO_SID'),
 'Account_token' => env('TWILIO_TOKEN'),
 'Account_from' => env('TWILIO_FROM'),

in SmsService you can access them as

     public function __contruct(){
        $this->account_sid = config('app.Account_SID');
        $this->account_token =config('app.Account_token');
        $this->account_from = config('app.Account_from');
        $this->reciever = new Client($this->account_sid, $this->account_token);
     }
    

You should never access env variables directly in your application, but only through config files.

The best place to put these is in config/services.php

Add a section for Twilio;

    'twilio' => [
        'sid' => env('TWILIO_SID'),
        'token => env('TWILIO_TOKEN'),
        'from' => env('TWILIO_FROM'),
    ] 

then open tinker and run

>>> config('services.twilio')

and check that the values are all represented there as expected.

Then in your service provider change references to env() for config and using the dot separated names. eg;

    public function __contruct(){
       $this->account_sid = config('services.twilio.sid');
       $this->account_token = config('services.twilio.token');
       $this->account_from = config('services.twilio.from);

finally, make sure you bind your class into the container via the register() method of a service provider.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM