Integrate Twilio Into Laravel Like a Senior Dev

I am a full-stack web developer that enjoys coding in Laravel and React.
Search for a command to run...

I am a full-stack web developer that enjoys coding in Laravel and React.
Very well-written article!
We can also extend this solution to make use of the built-in IoC/Dependency Injection in Laravel.
Thanks
Here I will post some articles that help devs with simple and complex topics in Laravel.
GraphQL, in my opinion, has been one of the best technologies to be released in the last ten years. GraphQL is a specification that describes how we can use just one query to get data from an API instead of hitting multiple endpoints with different H...
Switching stacks

If you don't know what Twin Macro is, check it out! It's a great library that blends Tailwind CSS and Styled Component systems, like Emotion. I love using Twin because it lets me use Tailwind while separating my styling from my markup and methods. If...

Many developers know that full-stack apps with a Vue frontend can be quickly spun up with Laravel Jetstream. What many don’t know is that recently, the Laravel team made it easy to make an Inertia app with Laravel Breeze. In this article, we'll make ...

Extending the Rule Object with a DateTime Trait

Using Custom Console Commands

In many tutorials around the web, authors use and define services like Twilio in the controller, which flies in the face of the principles of MVC frameworks like Laravel. It also doesn't heed DRY principles as defined in the book, The Pragmatic Programmer. In this tutorial, we'll discuss how you can implement third-party services like Twilio into your Laravel application, the right way.
Preliminaries:
composer require twilio/sdk in your project.TWILIO_SID, TWILIO_TOKEN, TWILIO_NUMBER.First Steps:
<?php
return [
...
'twilio' => [
'sid' => env('TWILIO_SID'),
'token' => env('TWILIO_TOKEN'),
'phone' => env('TWILIO_NUMBER')
]
]
The Work Begins:
Twilio\Rest\Client. We'll also need to set the namespace for our file so that we can access it elsewhere. <?php
namespace App\Services;
use Twilio\Rest\Client;
<?php
namespace App\Services;
use Twilio\Rest\Client;
class Twilio {
protected $sid;
protected $token;
protected $phone;
function __construct(){
$this->sid = config('services.twilio.sid');
$this->token = config('services.twilio.token');
$this->phone = config('services.twilio.phone');
}
}
The Work Continues:
...
protected $client;
function __construct(){
$this->sid = config('services.twilio.sid');
$this->token = config('services.twilio.token');
$this->phone = config('services.twilio.phone');
$this->client = new Client($this->sid, $this->token);
}
...
function __construct(){
$this->sid = config('services.twilio.sid');
$this->token = config('services.twilio.token');
$this->phone = config('services.twilio.phone');
$this->client = new Client($this->sid, $this->token);
}
public function send($recipient_phone, $message){
$sms = $this->client->messages->create($recipient_phone, [
'from' => $this->phone,
'body' => $message
]);
return $sms;
}
The End:
<?php
namespace App\Listeners;
use App\Events\SomeEvent;
use App\Services\Twilio;
class DoThisAfterSomeEvent
{
...
public function handle(SomeEvent $event){
...
$twilio = new Twilio();
$twilio->send($recipient_phone, $message);
}
}