norkunas / onesignal-php-api

OneSignal API for PHP
MIT License
233 stars 82 forks source link

where to put this code??! #114

Closed mfarhat437 closed 4 years ago

mfarhat437 commented 5 years ago

require DIR . '/vendor/autoload.php';

use GuzzleHttp\Client as GuzzleClient; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; use Http\Client\Common\HttpMethodsClient as HttpClient; use Http\Message\MessageFactory\GuzzleMessageFactory; use OneSignal\Config; use OneSignal\OneSignal;

$config = new Config(); $config->setApplicationId('your_application_id'); $config->setApplicationAuthKey('your_application_auth_key'); $config->setUserAuthKey('your_auth_key');

$guzzle = new GuzzleClient([ // http://docs.guzzlephp.org/en/stable/quickstart.html // ..config ]);

$client = new HttpClient(new GuzzleAdapter($guzzle), new GuzzleMessageFactory()); $api = new OneSignal($config, $client);

norkunas commented 5 years ago

In your application :)

norkunas commented 5 years ago

Are you familiar with composer? Do you use any frameworks?

mfarhat437 commented 5 years ago

Yes i use laravel On Feb 28, 2019 6:58 AM, "Tomas Norkūnas" notifications@github.com wrote:

Are you familiar with composer? Do you use any frameworks?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/norkunas/onesignal-php-api/issues/114#issuecomment-468137708, or mute the thread https://github.com/notifications/unsubscribe-auth/Apr80ynhmSrFygYKRBRf1WJVVmk04ns3ks5vR2HagaJpZM4bVLiz .

norkunas commented 5 years ago

So bind the OneSignal service to your container:

use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Client\Common\HttpMethodsClient as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use OneSignal\Config;
use OneSignal\OneSignal;

$this->app->bind('OneSignal\OneSignal', function ($app) {
    $config = new Config();
    $config->setApplicationId('your_application_id');
    $config->setApplicationAuthKey('your_application_auth_key');
    $config->setUserAuthKey('your_auth_key');

    $client = new HttpClient(new GuzzleAdapter(new GuzzleClient()), new GuzzleMessageFactory());

    return new OneSignal\OneSignal($config, $client);
});

And then inject OneSignal to your controller. For more docs read https://laravel.com/docs/5.8/container