devigner/kunstmaan-api-bundle
Installation
composer require devigner/kunstmaan-api-bundle
Ensure KunstmaanBundlesCMS is installed as a dependency.
Bundle Configuration
Add to config/bundles.php:
return [
// ...
Devigner\KunstmaanApiBundle\DevignerKunstmaanApiBundle::class => ['all' => true],
];
First Use Case: Expose a Page
PageModel class implementing PageModelInterface:
use Devigner\KunstmaanApiBundle\Model\PageEntityInterface;
class MyPageModel implements PageEntityInterface {
// Implement required methods (e.g., getTitle(), getContent())
}
Page entity to implement PageModelInterface:
use Devigner\KunstmaanApiBundle\Entity\PageModelInterface;
class MyPage implements PageInterface, PageModelInterface {
// Delegate API methods to MyPageModel
}
services.yaml:
services:
App\Model\MyPageModel:
tags: ['kunstmaan_api.page_model']
Page Exposure
PageEntityInterface to define API contracts (e.g., getTitle(), getSlug()).Serializer component (auto-configured) for JSON output.// In a controller
$page = $this->getDoctrine()->getRepository(MyPage::class)->find($id);
return $this->json($page->getApiModel()); // Returns PageModelInterface
PageParts Integration
PagePartsModelInterface for custom page parts:
class MyPagePartModel implements PagePartsModelInterface {
public function getData(): array {
return ['key' => 'value'];
}
}
PagePartInterface and bind the model:
class MyPagePart implements PagePartInterface, PagePartsModelInterface {
private MyPagePartModel $model;
public function __construct(MyPagePartModel $model) {
$this->model = $model;
}
public function getApiModel(): PagePartsModelInterface {
return $this->model;
}
}
Overview Pages (e.g., Newspages)
EntityInjectionInterface to customize API responses:
class NewsPage implements EntityInjectionInterface {
public function injectEntities(array $entities): array {
return array_map(fn($entity) => $entity->getApiModel(), $entities);
}
}
Slug Handling
SlugEventListener (auto-registered) ensures slugs are resolved for API routes.services.yaml:
Devigner\KunstmaanApiBundle\EventListener\SlugEventListener:
arguments:
- ['@my_custom_menu_service'] # Replace with your menu service
Missing Model Implementations
Class "App\Model\MyPageModel" does not implement "Devigner\KunstmaanApiBundle\Model\PageEntityInterface".getTitle(), getSlug()) are implemented. Use PageEntityInterface as a base class if possible.Circular Dependencies
Cannot autowire service "App\Entity\MyPage": Argument "$model" not found.services.yaml:
App\Entity\MyPage:
arguments:
$model: '@App\Model\MyPageModel'
Slug Resolution Failures
No menu found for slug resolution.kunstmaan_menu.menus service is properly configured in services.yaml or override the SlugEventListener arguments.Deprecated Kunstmaan Version
API Response Validation
Use Symfony’s Serializer debug tool to validate output:
php bin/console debug:serializer
Event Listener Debugging
Enable debug mode to trace SlugEventListener:
// In config/packages/dev.php
framework:
profiler: { only_exceptions: false }
Model Binding Dump model bindings to verify injection:
$page = $this->getDoctrine()->getRepository(MyPage::class)->find(1);
dump(get_class_methods($page->getApiModel())); // Check implemented methods
Custom Serialization Groups Annotate models to control serialized fields:
use Symfony\Component\Serializer\Annotation\Groups;
class MyPageModel implements PageEntityInterface {
#[Groups(['api'])]
public function getTitle(): string { ... }
}
API Route Customization Override the default API routes by extending the bundle’s routing loader:
# config/routes.yaml
kunstmaan_api:
resource: "@DevignerKunstmaanApiBundle/Resources/config/routing.yaml"
prefix: /api
defaults: { _controller: "App\Controller\ApiController::index" }
Dynamic Model Resolution Use a factory pattern to resolve models dynamically:
class PageModelFactory {
public function createFromEntity(PageInterface $page): PageEntityInterface {
return new MyPageModel($page->getTitle(), $page->getSlug());
}
}
How can I help you explore Laravel packages today?