1. Go to this page and download the library: Download mixerapi/json-ld-view 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/ */
mixerapi / json-ld-view example snippets
# src/Application.php
public function bootstrap(): void
{
// other logic...
$this->addPlugin('MixerApi/JsonLdView');
}
# src/Controller/AppController.php
public function initialize(): void
{
parent::initialize();
$this->loadComponent('RequestHandler');
// other logic...
}
# config/routes.php
$routes->scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['jsonld']);
// ... other code
});
# App/Model/Entity/Film.php
class Film extends Entity implements JsonLdDataInterface
{
// ...other code
/**
* This is the context URL that you defined in your routes during Setup. This is used to browse the schema
* definitions and appears as `@context` when displaying collection or item results
*
* @return string
*/
public function getJsonLdContext(): string
{
return '/contexts/Film';
}
/**
* This is the Entities schema description and appears as `@type` when displaying collection or item results
*
* @return string
*/
public function getJsonLdType(): string
{
return 'https://schema.org/movie';
}
/**
* This is the Entities URL and appears as `@id` when displaying collection or item results
*
* @param EntityInterface $entity
* @return string
*/
public function getJsonLdIdentifier(EntityInterface $entity): string
{
return '/films/' . $entity->get('id');
}
/**
* You can define custom schemas here. These definitions take precedence and will appear when browsing to the
* entities context URL. You can simply return an empty array if you don't care to define a schema.
*
* @return \MixerApi\JsonLdView\JsonLdSchema[]
*/
public function getJsonLdSchemas(): array
{
return [
new JsonLdSchema('title','https://schema.org/name', 'optional description')
new JsonLdSchema('description','https://schema.org/about')
new JsonLdSchema('length','https://schema.org/duration')
new JsonLdSchema('rating','https://schema.org/contentRating')
new JsonLdSchema('release_year','https://schema.org/copyrightYear')
];
}
}
#src/Controller/FilmsController.php
public function index()
{
$this->request->allowMethod('get');
$actors = $this->paginate($this->Films, [
'contain' => ['Languages'],
]);
$this->set(compact('films'));
$this->viewBuilder()->setOption('serialize', 'films');
}
#src/Controller/LanguagesController.php
public function view($id = null)
{
$this->request->allowMethod('get');
$languages = $this->Languages->get($id);
$this->set('languages', $languages);
$this->viewBuilder()->setOption('serialize', 'languages');
}
use MixerApi\JsonLdView\JsonSerializer;
# json
$json = (new JsonSerializer($data))->asJson(JSON_PRETTY_PRINT); // argument is optional
# array
$json = (new JsonSerializer($data))->getData();
# json-ld with pagination meta data
use Cake\Http\ServerRequest;
use Cake\View\Helper\PaginatorHelper;
$json = (new JsonSerializer($data, new ServerRequest(), new PaginatorHelper()))->asJson();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.