Installation
composer require croct/plug-symfony
Add the bundle to config/bundles.php:
return [
// ...
Croct\Plug\Symfony\CroctPlugBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console croct:plug:install
Update config/packages/croct_plug.yaml with your API key and environment (e.g., sandbox or production).
First Use Case
Inject the CroctPlugClient into a controller/service and fetch dynamic content:
use Croct\Plug\Symfony\Client\CroctPlugClient;
class MyController extends AbstractController
{
public function __construct(private CroctPlugClient $client) {}
public function showContent(): Response
{
$response = $this->client->getContent('your-content-id');
return $this->render('template.html.twig', [
'content' => $response->getContent(),
]);
}
}
Twig Integration
Enable Twig extensions in config/packages/twig.yaml:
twig:
globals:
croct: '@croct.plug.twig_extension'
Use in templates:
{{ croct.render('your-content-id') }}
Content Fetching
CroctPlugClient for real-time requests (e.g., in controllers).
$this->client->getContent('id', ['userId' => 123]);
$cache = $this->client->getCache();
$content = $cache->get('content:id', fn() => $this->client->getContent('id'));
Personalization
Pass user context via getContent() options:
$this->client->getContent('id', [
'userId' => $user->id,
'traits' => ['premium' => true],
]);
Event-Driven Updates
Subscribe to Croct’s webhooks (via CroctPlugWebhookEventSubscriber) to handle real-time content updates:
# config/packages/croct_plug.yaml
croct_plug:
webhooks:
enabled: true
secret: 'your-webhook-secret'
Dependency Injection
Prefer constructor injection for CroctPlugClient and CroctPlugCache:
public function __construct(
private CroctPlugClient $client,
private CroctPlugCache $cache
) {}
Environment-Specific Config Use Symfony’s parameter bags for environment variables:
# config/packages/croct_plug.yaml
croct_plug:
api_key: '%env(CROCT_API_KEY)%'
environment: '%kernel.environment%'
Error Handling
Wrap Croct calls in try-catch blocks to handle CroctPlugException:
try {
$response = $this->client->getContent('id');
} catch (CroctPlugException $e) {
$this->addFlash('error', $e->getMessage());
return $this->redirectToRoute('home');
}
Testing
Mock CroctPlugClient in PHPUnit:
$mock = $this->createMock(CroctPlugClient::class);
$mock->method('getContent')->willReturn(new CroctPlugResponse(['html' => '<p>Mocked</p>']));
$this->controller->setClient($mock);
Performance
CroctPlugCache with a short TTL (e.g., 5 minutes) for frequently accessed content.$this->client->getContents(['id1', 'id2']);
API Key Exposure
%env() or .env files:
croct_plug:
api_key: '%env(CROCT_API_KEY)%'
Rate Limiting
CroctPlugException for 429 Too Many Requests errors.Twig Auto-escaping
|raw if needed:
{{ croct.render('id')|raw }}
Webhook Verification
CroctPlugWebhookValidator:
$validator = new CroctPlugWebhookValidator('your-secret');
if (!$validator->validate($request->getContent(), $request->headers)) {
throw new \RuntimeException('Invalid webhook signature');
}
Enable Logging
Configure Monolog in config/packages/monolog.yaml:
monolog:
handlers:
croct:
type: stream
path: "%kernel.logs_dir%/croct.log"
level: debug
Croct logs will include request/response details and errors.
HTTP Debugging
Use Symfony’s PROFILER to inspect Croct HTTP calls:
_PROFILE=1 php bin/console server:run
Common Errors
| Error | Cause | Solution |
|---|---|---|
Invalid API Key |
Wrong api_key in config. |
Verify .env or config file. |
Content Not Found |
Invalid contentId. |
Check Croct Dashboard for correct IDs. |
Network Timeout |
Croct API downtime. | Retry with exponential backoff. |
Twig Error: Undefined method |
Twig extension not registered. | Ensure croct_plug.twig_extension is in globals. |
Custom Cache Adapter
Extend CroctPlugCache to use Redis or Doctrine Cache:
use Croct\Plug\Symfony\Cache\CroctPlugCache;
use Symfony\Contracts\Cache\CacheInterface;
class RedisCroctPlugCache extends CroctPlugCache
{
public function __construct(CacheInterface $cache) {
parent::__construct($cache);
}
}
Content Preprocessing
Hook into CroctPlugResponse to modify content before rendering:
$response = $this->client->getContent('id');
$response->setContent(
str_replace('{{placeholder}}', 'Custom Value', $response->getContent())
);
Event Listeners
Subscribe to CroctPlugEvents (e.g., CONTENT_FETCHED) for analytics:
use Croct\Plug\Symfony\Event\CroctPlugEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CroctAnalyticsSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CroctPlugEvents::CONTENT_FETCHED => 'logContentFetch',
];
}
public function logContentFetch(CroctPlugEvent $event) {
// Send analytics to your service.
}
}
Custom Content Types
Extend CroctPlugClient to support non-HTML responses (e.g., JSON):
$client->getContent('id', [], CroctPlugClient::FORMAT_JSON);
How can I help you explore Laravel packages today?