Easier Laravel integration for rocket-code/shopify
, (*1)
This packages allows for a better integration of rocket-code/shopify in Laravel applications., (*2)
Benefits
By using this package you get these added values:, (*3)
- Auto discovery (Laravel 5.5 or higher) - no need to manually add any service provider
- Configuration in env file
- Allow the service to be used in dependency injection
- Allow usage as real time facade (Laravel 5.4 or higher)
Installation
You can install the package via composer:, (*4)
composer require boaideas/laravel-shopify
If you're installing the package on Laravel 5.5 or higher, you're done (The package uses Laravel's auto package discovery)., (*5)
If you're using Laravel 5.4 or less, add the BOAIdeas\Shopify\ShopifyServiceProvider
service provider to your providers array:, (*6)
// config/app.php
'providers' => [
...
BOAIdeas\Shopify\ShopifyServiceProvider::class,
];
Configuration
Now, by default, the package will look for the following values in your .env file:, (*7)
// .env
SHOPIFY_KEY=YourAppApiKey
SHOPIFY_SECRET=YourAppSecret
SHOPIFY_DOMAIN=YourShopDomain (for private apps)
SHOPIFY_TOKEN=YourToken
If, for some reason, you want to change any of these settings, you can publish the config file with:, (*8)
php artisan vendor:publish --provider="BOAIdeas\Shopify\ShopifyServiceProvider"
This is the content of the published config file:, (*9)
// config/shopify.php
return [
'api_key' => env('SHOPIFY_KEY'),
'api_secret' => env('SHOPIFY_SECRET'),
'shop_domain' => env('SHOPIFY_DOMAIN'),
'access_token' => env('SHOPIFY_TOKEN'),
];
Usage
Once installed, you can use the service by either injecting it to your methods or as a real time facade, and then just use it regularly., (*10)
For more information about how to use the service, look at https://github.com/joshrps/laravel-shopify-API-wrapper., (*11)
Dependency Injection
Now you can simply type hint the service in your method's arguments. For better readabilty, we prefer to import the full class name with a use
statement, and alias it to Shopify while we're at it., (*12)
use RocketCode\Shopify\API as Shopify;
Route::get('/', function (Shopify $shopify) {
$call = $shopify->call(
[
'URL' => 'products.json',
'METHOD' => 'GET',
'DATA' => [
'limit' => 5,
'published_status' => 'any'
]
]);
});
Facade
Now you can use Laravel's on the fly facades feature to use the service "statically". We prefer to alias it to ShopifyAPI while we're at it., (*13)
use Facades\RocketCode\Shopify\API as ShopifyAPI;
Route::get('/', function () {
$call = ShopifyAPI::call(
[
'URL' => 'products.json',
'METHOD' => 'GET',
'DATA' => [
'limit' => 5,
'published_status' => 'any'
]
]);
});
Credits
License
The MIT License (MIT). Please see License File for more information., (*14)