Install the Bundle Run:
composer require anh/sape-bundle:~2.0
Ensure anh/sape-php-client (dependency) is also installed.
Enable the Bundle
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony <4):
Anh\SapeBundle\AnhSapeBundle::class => ['all' => true],
Configure SAPE
Add your credentials to config/packages/anh_sape.yaml (Symfony 4+) or config.yml (Symfony <4):
anh_sape:
user: 'your_sape_user_id_here' # e.g., '80e20e4a72a1d09895763b4b1bc98e63'
client_options: { charset: 'utf8' } # Optional; defaults to utf8
First Use Case Generate SAPE links in Twig:
{% set links = sape_return_links() %}
{% for link in links %}
<a href="{{ link.url }}">{{ link.title }}</a>
{% endfor %}
Dynamic Link Generation
sape_return_links() in Twig to fetch and render SAPE links for a page.{% set cachedLinks = cache(app.cache, 'sape_links', 3600) %}
{% if not cachedLinks %}
{% set cachedLinks = sape_return_links() %}
{% do cache(app.cache, 'sape_links', cachedLinks, 3600) %}
{% endif %}
Block-Specific Links
sape_return_block_links() to target specific content blocks (e.g., articles, products):
{{ sape_return_block_links(blockId) }}
Controller Integration
// src/Controller/PageController.php
public function show(Page $page): Response
{
$sapeLinks = $this->get('anh_sape.client')->getLinks();
return $this->render('page/show.html.twig', [
'links' => $sapeLinks,
]);
}
Event-Driven Updates
// src/EventListener/SapeUpdateListener.php
public function onContentUpdate(ContentEvent $event)
{
$this->get('anh_sape.client')->updateLinks();
}
{{ form_row(form.field) }}
<div class="sape-links">
{{ sape_return_block_links('form_field_' ~ form.vars.id) }}
</div>
// src/Controller/SapeApiController.php
public function getLinks(): JsonResponse
{
return $this->json($this->get('anh_sape.client')->getLinks());
}
// src/Twig/SapeExtension.php
public function getFilteredLinks(array $filters): array
{
return $this->client->getLinks()->filter($filters);
}
User ID Validation
user ID in config. Test with:
php bin/console debug:config anh_sape
$client = $this->get('anh_sape.client');
var_dump($client->getLinks()); // Should not be empty
Character Encoding
charset: utf8 may fail for non-UTF8 pages. Override in config:
client_options: { charset: 'windows-1251' }
Caching Conflicts
# config/packages/cache.yaml
app.cache.sape:
adapter: cache.adapter.apcu
provider: 'cache.provider.doctrine'
default_lifetime: 300 # 5 minutes
Twig Function Scope
sape_return_links() is Twig-only. For PHP, use:
$links = $this->get('anh_sape.client')->getLinks();
Log SAPE Responses
Enable debug mode in config/packages/dev/anh_sape.yaml:
anh_sape:
debug: true
Check logs for raw SAPE responses:
grep -i "sape" var/log/dev.log
Test Locally Use a local SAPE sandbox user ID to avoid live API calls during development.
Custom Link Filters Extend the client to filter links by type/language:
// src/Service/SapeClient.php
public function getLinksByType(string $type): array
{
return array_filter($this->getLinks(), fn($link) => $link->type === $type);
}
Async Processing Offload SAPE updates to a queue (e.g., Symfony Messenger):
// src/Message/SapeUpdateMessage.php
public function __construct(private string $contentId) {}
Multi-Account Support
Override the bundle’s SAPE_client to support multiple SAPE accounts:
// src/DependencyInjection/AnhSapeExtension.php
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('anh_sape.users', $config['users']);
}
Webhook Integration Listen for SAPE link updates via webhooks:
// src/EventListener/SapeWebhookListener.php
public function onWebhook(Request $request)
{
$data = json_decode($request->getContent(), true);
$this->get('anh_sape.client')->updateFromWebhook($data);
}
How can I help you explore Laravel packages today?