Install the Bundle Add the package via Composer:
composer require benji07/akismet-bundle
Enable the bundle in config/bundles.php:
Benji07\AkismetBundle\Benji07AkismetBundle::class => ['all' => true],
Configure Akismet
Add your Akismet API key and blog URL in config/packages/benji_akismet.yaml:
benji_akismet:
key: 'your_akismet_api_key'
blog: 'https://your-site.com'
First Use Case: Spam Detection
Inject the Akismet service into a controller or service:
use Benji07\AkismetBundle\Akismet\Akismet;
public function __construct(private Akismet $akismet) {}
public function checkComment(Comment $comment)
{
$data = [
'user_ip' => $comment->getUserIp(),
'user_agent' => $comment->getUserAgent(),
'referrer' => $comment->getReferrer(),
'comment_type' => 'comment',
'comment_author' => $comment->getUsername(),
'comment_author_email' => $comment->getEmail(),
'comment_content' => $comment->getContent(),
];
return $this->akismet->isSpam($data);
}
Spam Detection in Forms
Use the isSpam() method to validate user-submitted content (e.g., comments, contact forms):
public function store(Request $request)
{
$data = $this->mapFormData($request);
if ($this->akismet->isSpam($data)) {
return back()->with('error', 'Spam detected!');
}
// Save to DB
}
Feedback Loop for False Positives/Negatives Submit spam/ham feedback to improve Akismet’s model:
// After manual review
if ($comment->isSpam) {
$this->akismet->submitSpam($data);
} else {
$this->akismet->submitHam($data);
}
Event-Driven Integration
Trigger Akismet checks via Symfony events (e.g., kernel.request):
// src/EventListener/AkismetListener.php
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMainRequest() && $event->getRequest()->isMethod('POST')) {
$data = $this->extractFormData($event->getRequest());
if ($this->akismet->isSpam($data)) {
$event->setResponse(new Response('Spam blocked', 403));
}
}
}
Caching Responses Cache Akismet results to reduce API calls (e.g., using Symfony’s cache system):
$cacheKey = 'akismet_' . md5($comment->getContent());
$isSpam = $this->cache->get($cacheKey);
if ($isSpam === null) {
$isSpam = $this->akismet->isSpam($data);
$this->cache->set($cacheKey, $isSpam, '1 hour');
}
API Key Validation
try {
$this->akismet->verifyKey();
} catch (\Exception $e) {
throw new \RuntimeException('Invalid Akismet API key');
}
Rate Limits
Missing Fields
isSpam() requires all fields (e.g., user_ip, comment_content). Omit optional fields (e.g., referrer) but avoid passing null:
$data = array_filter([
'user_ip' => $comment->getUserIp(),
'comment_content' => $comment->getContent(),
// 'referrer' => null, // Skip instead of passing null
]);
Symfony 4+ Deprecations
config/services.yaml:
services:
Benji07\AkismetBundle\Akismet\Akismet: ~
Enable Verbose Logging Configure Monolog to log Akismet requests/responses:
# config/packages/monolog.yaml
handlers:
akismet:
type: stream
path: "%kernel.logs_dir%/akismet.log"
level: debug
channels: ["akismet"]
Then enable the channel in the bundle’s service.
Test Locally Use Akismet’s sandbox API for testing:
benji_akismet:
key: 'your_sandbox_key'
blog: 'http://example.com'
Custom Data Mappers Create a decorator to transform data before sending to Akismet:
// src/Service/AkismetDataMapper.php
class AkismetDataMapper implements DataMapperInterface
{
public function map($entity): array
{
$data = [
'user_ip' => $entity->getIp(),
'comment_content' => $entity->getContent(),
];
// Add custom logic (e.g., sanitize content)
return $data;
}
}
Override HTTP Client Replace the default Guzzle client for custom headers/proxies:
benji_akismet:
client:
options:
headers:
'X-Custom-Header': 'value'
proxy: 'http://proxy.example.com'
Add Custom Fields
Extend the bundle’s configuration to support additional Akismet fields (e.g., user_role):
// src/DependencyInjection/Benji07AkismetExtension.php
public function load(array $configs, ContainerBuilder $container)
{
$container->setParameter('akismet.custom_fields', ['user_role']);
}
How can I help you explore Laravel packages today?