PHP code example of craftcms / element-api
1. Go to this page and download the library: Download craftcms/element-api library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
craftcms / element-api example snippets
use craft\elements\Entry;
use craft\helpers\UrlHelper;
return [
'endpoints' => [
'news.json' => function() {
return [
'elementType' => Entry::class,
'criteria' => ['section' => 'news'],
'transformer' => function(Entry $entry) {
return [
'id' => $entry->id,
'title' => $entry->title,
'url' => $entry->url,
'jsonUrl' => UrlHelper::url("news/$entry->id.json"),
'summary' => $entry->summary,
];
},
];
},
'news/<entryId:\d+>.json' => function($entryId) {
return [
'elementType' => Entry::class,
'criteria' => ['id' => $entryId],
'one' => true,
'transformer' => function(Entry $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'summary' => $entry->summary,
'body' => $entry->body,
];
},
];
},
]
];
'elementType' => craft\elements\Entry::class,
'criteria' => [
'section' => 'news',
'type' => 'article',
],
'contentType' => 'application/foo+json',
// Can be set to a function
'transformer' => function(craft\elements\Entry $entry) {
return [
'title' => $entry->title,
'id' => $entry->id,
'url' => $entry->url,
];
},
// Or a string/array that defines a Transformer class configuration
'transformer' => 'MyTransformerClassName',
// Or a Transformer class instance
'transformer' => new MyTransformerClassName(),
use craft\elements\Entry;
use League\Fractal\TransformerAbstract;
class MyTransformerClassName extends TransformerAbstract
{
public function transform(Entry $entry)
{
return [
// ...
];
}
}
'one' => true,
'paginate' => false,
'elementsPerPage' => 10,
'pageParam' => 'pg',
'resourceKey' => 'entries',
'meta' => [
'description' => 'Recent news from Happy Lager',
],
'
class MyTransformerClassName extends TransformerAbstract
{
protected $availableIncludes = ['author'];
public function ->id,
'name' => $author->name,
];
});
}
// ...
}
'excludes' => 'author',
'callback' => 'foo',
'jsonOptions' => JSON_UNESCAPED_UNICODE,
'pretty' => true,
'cache' => 'PT1M', // one minute
'news/<entryId:\d+>.json' => function($entryId) {
return [
'elementType' => craft\elements\Entry::class,
'criteria' => ['id' => $entryId],
'one' => true,
];
},
use craft\elements\Entry;
return [
'defaults' => [
'elementType' => Entry::class,
'elementsPerPage' => 10,
'pageParam' => 'pg',
'transformer' => function(Entry $entry) {
return [
'title' => $entry->title,
'id' => $entry->id,
'url' => $entry->url,
];
},
],
'endpoints' => [
'news.json' => function() {
return [
'criteria' => ['section' => 'news'],
]
},
'news/<entryId:\d+>.json' => function($entryId) {
return [
'criteria' => ['id' => $entryId],
'one' => true,
];
},
]
];
'ingredients.json' => function() {
return [
'criteria' => ['section' => 'ingredients'],
'elementsPerPage' => 10,
'transformer' => function(craft\elements\Entry $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'jsonUrl' => UrlHelper::url("ingredients/$entry->slug.json"),
];
},
'pretty' => true,
];
},
'ingredients/<slug:{slug}>.json' => function($slug) {
return [
'criteria' => [
'section' => 'ingredients',
'slug' => $slug
],
'one' => true,
'transformer' => function(craft\elements\Entry $entry) {
// Create an array of all the photo URLs
$photos = [];
foreach ($entry->photos->all() as $photo) {
$photos[] = $photo->url;
}
return [
'title' => $entry->title,
'url' => $entry->url,
'description' => (string)$entry->description,
'photos' => $photos
];
},
'pretty' => true,
];
},
'feed.json' => function() {
return [
'serializer' => 'jsonFeed',
'elementType' => craft\elements\Entry::class,
'criteria' => ['section' => 'news'],
'transformer' => function(craft\elements\Entry $entry) {
$image = $entry->photos->one();
return [
'id' => (string)$entry->id,
'url' => $entry->url,
'title' => $entry->title,
'content_html' => (string)$entry->body,
'summary' => $entry->summary,
'image' => $image ? $image->url : null,
'date_published' => $entry->postDate->format(\DateTime::ATOM),
'date_modified' => $entry->dateUpdated->format(\DateTime::ATOM),
'authors' => [
['name' => $entry->author->name],
],
'language' => $entry->getSite()->language,
'tags' => array_map('strval', $entry->tags->all()),
];
},
'meta' => [
'description' => 'Recent news from Happy Lager',
],
'pretty' => true,
];
},
javascript
foo({ /* ... */ });