2

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?

Shahrukh
  • 399
  • 2
  • 15

3 Answers3

3

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.

Snapey
  • 3,604
  • 1
  • 21
  • 19
  • I think it's important to mention, that if you use `php artisan config:cache` the env variables will only updated if you run this command again. – Diverti Jan 11 '23 at 08:51
0

 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 
Ramil Huseynov
  • 350
  • 4
  • 7
  • Thanks for your response. I have already tried this but still, it's giving null. – Shahrukh Jul 01 '22 at 09:42
  • php artisan config:clear try pls – Ramil Huseynov Jul 01 '22 at 14:09
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 02 '22 at 10:08
0

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);
     }
    
Waqas Altaf
  • 392
  • 3
  • 16
  • Thanks. I have tried this is not working. As per laravel documentation Service Container did not bind config values implicitly. – Shahrukh Jul 01 '22 at 09:45