adback/adback-sdk-php-symfony
Symfony bundle for the AdBack API PHP SDK. Configure your access token and choose a cache backend (Redis, Doctrine, or a custom ScriptCacheInterface driver) to store SDK data. Supports Symfony 4+ auto-bundle activation.
Installation:
composer require adback/adback-sdk-php-symfony
For Symfony < 4, manually register the bundle in AppKernel.php:
new Adback\ApiClientBundle\Adback\ApiClientBundle(),
Configuration:
Add these to .env:
ADBACK_API_KEY=your_api_key_here
ADBACK_API_SECRET=your_api_secret_here
ADBACK_API_URL=https://api.adback.co
First Use Case:
Inject the AdbackApiClient service into a controller or service and call the API:
use Adback\ApiClientBundle\Service\AdbackApiClient;
public function someAction(AdbackApiClient $adback)
{
$response = $adback->getAdBlockStatus(); // Example method (verify actual API methods)
return json_encode($response);
}
Where to Look First:
config/packages/adback.yaml for default settings.src/Service/AdbackApiClient.php for available methods.Detecting AdBlock Users:
public function checkAdBlock(Request $request, AdbackApiClient $adback)
{
$isBlocked = $adback->isAdBlocked($request);
if ($isBlocked) {
// Serve alternative content or log the user
}
}
Tracking Events:
public function trackEvent(AdbackApiClient $adback, string $eventName, array $data = [])
{
$adback->trackEvent($eventName, $data);
}
Middleware for Global Checks: Create a middleware to auto-check ad-block status on every request:
namespace App\Http\Middleware;
use Adback\ApiClientBundle\Service\AdbackApiClient;
use Closure;
class AdbackMiddleware
{
public function __construct(private AdbackApiClient $adback) {}
public function handle($request, Closure $next)
{
if ($this->adback->isAdBlocked($request)) {
// Redirect or block access
}
return $next($request);
}
}
Register in config/routes.php or kernel.php.
Caching Responses: Cache API responses to reduce calls (e.g., ad-block status):
$cacheKey = 'adback_status_' . $request->getClientIp();
$isBlocked = Cache::remember($cacheKey, 3600, function() use ($adback, $request) {
return $adback->isAdBlocked($request);
});
Symfony Events:
Listen to kernel.request to log ad-block status globally:
$eventDispatcher->addListener('kernel.request', function (GetResponseEvent $event) {
$adback = $this->container->get(AdbackApiClient::class);
$request = $event->getRequest();
$event->setResponse(new Response($adback->isAdBlocked($request) ? 'blocked' : 'allowed'));
});
Twig Integration: Pass ad-block status to templates:
return $this->render('home.html.twig', [
'isAdBlocked' => $adback->isAdBlocked($request),
]);
Command-Line Usage: Create a console command for bulk checks:
namespace App\Command;
use Adback\ApiClientBundle\Service\AdbackApiClient;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CheckAdBlockCommand extends Command
{
protected static $defaultName = 'adback:check';
public function __construct(private AdbackApiClient $adback) {}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Checking ad-block status...');
$output->writeln('Status: ' . ($this->adback->isAdBlocked($request) ? 'Blocked' : 'Allowed'));
return Command::SUCCESS;
}
}
Deprecated Package:
adback/adback-sdk-php).Symfony 4+ Auto-Registration:
config/bundles.php:
return [
// ...
Adback\ApiClientBundle\Adback\ApiClientBundle::class => ['all' => true],
];
Missing Documentation:
AdbackApiClient class lacks method signatures in the README. Inspect the source (src/Service/AdbackApiClient.php) for actual methods (e.g., isAdBlocked(), trackEvent()).GET /status for checks).Error Handling:
try {
$response = $adback->someMethod();
} catch (\Exception $e) {
// Log or fallback (e.g., assume ad-block is off)
return false;
}
Configuration Overrides:
config/packages/adback.yaml:
adback:
api_url: 'https://custom.adback.api'
api_key: '%env(ADBACK_CUSTOM_KEY)%'
Enable API Logging:
Add a logger to AdbackApiClient:
public function __construct(
private ClientInterface $client,
private LoggerInterface $logger
) {}
public function isAdBlocked(Request $request)
{
$this->logger->debug('Checking ad-block for IP: ' . $request->getClientIp());
// ...
}
Verify Requests:
Use Symfony’s Profiler or HttpClient middleware to inspect outgoing API calls:
$client = new Client([
'headers' => ['X-Adback-Key' => $this->apiKey],
]);
Test Locally:
Mock the AdbackApiClient in tests:
$mock = $this->createMock(AdbackApiClient::class);
$mock->method('isAdBlocked')->willReturn(true);
$this->container->set(AdbackApiClient::class, $mock);
Custom API Clients:
Extend AdbackApiClient to add methods:
namespace App\Service;
use Adback\ApiClientBundle\Service\AdbackApiClient;
class CustomAdbackClient extends AdbackApiClient
{
public function getUserStats(string $userId)
{
return $this->client->request('GET', '/users/' . $userId . '/stats');
}
}
Event Dispatching: Trigger Symfony events after API calls:
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
public function __construct(
private ClientInterface $client,
private EventDispatcherInterface $dispatcher
) {}
public function isAdBlocked(Request $request)
{
$result = $this->client->request('GET', '/status');
$this->dispatcher->dispatch(new AdBlockEvent($request, $result));
return $result['blocked'];
}
PSR-15 Middleware: Integrate with HTTP middleware for granular control:
namespace App\Http\Middleware;
use Adback\ApiClientBundle\Service\AdbackApiClient;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
class AdbackMiddleware implements MiddlewareInterface
{
public function __construct(private AdbackApiClient $adback) {}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->adback->isAdBlocked($request)) {
return new Response('Ad-block detected', 403);
}
return $handler->handle($request);
}
}
Database Integration: Store ad-block status in the database for analytics:
public function logAd
How can I help you explore Laravel packages today?