Created
July 4, 2018 03:00
-
-
Save shawnlindstrom/42443d090e6d56a5a93c3ec90c46935b to your computer and use it in GitHub Desktop.
Twilio Service Provider for Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'] | |
); | |
}); | |
} | |
} |
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
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.