Creating a Service Provider in Laravel for a Vendor Package
I’ve installed the following package into laravel:
https://github.com/omnilance/graphql-php-client
I want to bind the GraphQL class in the service provider with some config values and access it like:
app(OmnilanceGraphQLClient::class)->response()
Below is what I have so far. How would I set the host and other options the class provides? Do I do it in the boot method?
<?php
namespace AppProviders;
use OmnilanceGraphQLClient as GraphQL;
use IlluminateSupportServiceProvider;
class GraphQLServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// $client->setHost($app['config']->get('graphql.host'));
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind(OmnilanceGraphQLClient::class, function($app) {
return new GraphQL(
$app['config']->get('graphql.token')
);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [OmnilanceGraphQLClient::class];
}
}
from Laravel Questions and Answers https://laravelquestions.com/laravel/creating-a-service-provider-in-laravel-for-a-vendor-package/
via Lzo Media
No comments:
Post a Comment