Skip to content

Instantly share code, notes, and snippets.

@shawnlindstrom
Created July 4, 2018 03:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnlindstrom/42443d090e6d56a5a93c3ec90c46935b to your computer and use it in GitHub Desktop.
Save shawnlindstrom/42443d090e6d56a5a93c3ec90c46935b to your computer and use it in GitHub Desktop.
Twilio Service Provider for Laravel
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Twilio\Rest\Client as TwilioService;
class TwilioServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(TwilioService::class, function ($app) {
return new TwilioService(
$app->config['services.twilio.sid'],
$app->config['services.twilio.token']
);
});
}
}
@shawnlindstrom
Copy link
Author

shawnlindstrom commented Jul 4, 2018

Why?

You don't need a fancy package to use Twilio in your Laravel applications. Just toss a service provider in there and you can easily leverage the Twilio PHP SDK. Twilio has fantastic documentation which will make the rest of using Twilio in your application a cinch.

How

  1. composer require twilio/sdk
  2. Add your Twilio sid and token to config/services.php
  3. Create the TwilioServiceProvider in app/Providers
  4. Add the provider to your config/app.php
  5. Use constructor injection to resolve a client

Example

<?php

namespace App\Http\Controllers;

use Twilio\Rest\Client as TwilioService

class FooController extends Controller
{
    private $twilio;

    public function __construct(TwilioService $twilio) 
    {
        $this->twilio = $twilio;
    }

    public function call($to)
    {
        $call = $this->twilio->calls->create(
            config('services.twilio.from_number'), 
            $to,
            ['url' => 'http://demo.twilio.com/docs/voice.xml']
        );
    }
}

@jagroop
Copy link

jagroop commented Mar 6, 2019

Thanks :)

@jeremyhodges
Copy link

Any idea how I can turn that into a Facade and just use Twilio::calls->create() anywhere in my app? I've been trying to get it to work the last couple of hours and am clearly missing something.

@shawnlindstrom
Copy link
Author

Any idea how I can turn that into a Facade and just use Twilio::calls->create() anywhere in my app? I've been trying to get it to work the last couple of hours and am clearly missing something.

One idea would be to skip the service provider and just instantiate it inside a class then use a real-time facade:

<?php

namespace App;

use Twilio\Rest\Client;

class Call
{
     public function createCall($to, $options = [])
     {
          return (new Client(config.('twilio.sid'), config('twilio.token')))->calls->create($to, config('twilio.from'), $options);
     }
}

Then you could use it like so:

<?php

namespace App\Http\Controllers;

use Facades\App\Call;

class OutboundCallController extends Controller
{
     public function store()
     {
          $options = [
               // options like method, url, statusCallback, etc.
          ];
          Call::createCall('+17135551212', $options);     
     }
}

^The controller is contrived solely for the sake of an example. You'll find you end up having to do something like this if you ever get to working with Twilio subaccounts anyway. Generally, I'm making more specific types of calls so I just use a service class and call static methods. But, if you're only using your master account, just use the service provider and DI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment