-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using the library outside Laravel #74
Comments
This library is framework agnostic and includes some extras for easier usage with Laravel. It also uses some components of Laravel under the hood. I know some people who succesfully use it in other frameworks such as Drupal. Currently we do not have any examples, sorry. |
Perhaps this is usefull; a base PHP file and composer.json to get things working :) <?php
require '../vendor/autoload.php';
use Swis\JsonApi\Client\Parsers\CollectionParser;
use Swis\JsonApi\Client\Parsers\DocumentParser;
use Swis\JsonApi\Client\Parsers\ErrorCollectionParser;
use Swis\JsonApi\Client\Parsers\ErrorParser;
use Swis\JsonApi\Client\Parsers\ItemParser;
use Swis\JsonApi\Client\Parsers\JsonapiParser;
use Swis\JsonApi\Client\Parsers\LinksParser;
use Swis\JsonApi\Client\Parsers\MetaParser;
use Swis\JsonApi\Client\TypeMapper;
$apiClient = new \Swis\JsonApi\Client\Client();
// I choose not to set base url because the "NEXT" links normally contain the whole url
// $apiClient->setBaseUri($baseurl);
/**
* @return \Swis\JsonApi\Client\Parsers\ResponseParser
*/
function createResponseParser() {
$metaParser = new MetaParser();
$linksParser = new LinksParser($metaParser);
$itemParser = new ItemParser(new TypeMapper(), $linksParser, $metaParser);
$errorCollectionParser = new ErrorCollectionParser(
new ErrorParser($linksParser, $metaParser)
);
$documentParser = new DocumentParser(
$itemParser,
new CollectionParser($itemParser),
$errorCollectionParser,
$linksParser,
new JsonapiParser($metaParser),
$metaParser
);
return new \Swis\JsonApi\Client\Parsers\ResponseParser(
$documentParser
);
}
$client = new \Swis\JsonApi\Client\DocumentClient($apiClient, createResponseParser());
$endpointUrl = 'https://cms.contentacms.io/api/recipes';
$response = $client->get($endpointUrl);
/** @var Swis\JsonApi\Client\Collection $collection */
$collection = $response->getData();
// While there is a "next" link there are more pages
while ($response->getLinks()->offsetExists('next')) {
$response = $client->get($response->getLinks()->offsetGet('next')->getHref());
// Merge the next page with the current collection
$collection = $collection->merge($response->getData());
}
/** @var \Swis\JsonApi\Client\Item $item */
foreach ($collection as $item) {
// Do stuff with the items
if ($item->hasAttribute('title')) {
echo $item->title . '<br>';
}
} And the composer.json {
"name": "bbrala/base-jsonapi-client",
"require": {
"swisnl/json-api-client": "^1.1",
"guzzlehttp/guzzle": "^7.0"
},
"authors": [
{
"name": "Björn Brala",
"email": "bjorn@swis.nl"
}
]
} |
Seeing this example I think it will be nice to have some kind of factory to setup the ResponseParser so you don't have to do all that yourself if you don't use a container. |
Yeah we need to have a factory for creating the base client. That would make this only a few lines long. |
Anothing think i notice, i feel |
That's a fair point. |
I've added some factory methods to make it easier to use this client outside of Laravel without an IOC container. See https://github.com/swisnl/json-api-client#getting-started for a basic example. Please feel free to report any issues you run into! |
Is it possible to use the library in a framework agnostic way? I am building a package which consumes a json api, and I'd love to take advantage of this package, but it is not clear if it requiere laravel or can be used without it.
If the anwser is true, is there some examples? Thank you so much!
The text was updated successfully, but these errors were encountered: