Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Adback Sdk Php Symfony Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require adback/adback-sdk-php-symfony
    

    For Symfony < 4, manually register the bundle in AppKernel.php:

    new Adback\ApiClientBundle\Adback\ApiClientBundle(),
    
  2. 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
    
  3. 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);
    }
    
  4. Where to Look First:

    • Check config/packages/adback.yaml for default settings.
    • Review src/Service/AdbackApiClient.php for available methods.
    • Refer to AdBack API docs for endpoint specifics.

Implementation Patterns

Common Workflows

  1. Detecting AdBlock Users:

    public function checkAdBlock(Request $request, AdbackApiClient $adback)
    {
        $isBlocked = $adback->isAdBlocked($request);
        if ($isBlocked) {
            // Serve alternative content or log the user
        }
    }
    
  2. Tracking Events:

    public function trackEvent(AdbackApiClient $adback, string $eventName, array $data = [])
    {
        $adback->trackEvent($eventName, $data);
    }
    
  3. 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.

  4. 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);
    });
    

Integration Tips

  • 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;
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • Last release in 2018 may indicate outdated API compatibility. Verify endpoints against AdBack’s current API.
    • Check for breaking changes in newer AdBack SDKs (e.g., adback/adback-sdk-php).
  2. Symfony 4+ Auto-Registration:

    • The bundle may not auto-register in Symfony 5+ due to age. Manually add to config/bundles.php:
      return [
          // ...
          Adback\ApiClientBundle\Adback\ApiClientBundle::class => ['all' => true],
      ];
      
  3. Missing Documentation:

    • The AdbackApiClient class lacks method signatures in the README. Inspect the source (src/Service/AdbackApiClient.php) for actual methods (e.g., isAdBlocked(), trackEvent()).
    • Assume methods follow REST conventions (e.g., GET /status for checks).
  4. Error Handling:

    • The bundle may not include robust error handling. Wrap API calls in try-catch:
      try {
          $response = $adback->someMethod();
      } catch (\Exception $e) {
          // Log or fallback (e.g., assume ad-block is off)
          return false;
      }
      
  5. Configuration Overrides:

    • Customize API URL/credentials in config/packages/adback.yaml:
      adback:
          api_url: 'https://custom.adback.api'
          api_key: '%env(ADBACK_CUSTOM_KEY)%'
      

Debugging Tips

  1. 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());
        // ...
    }
    
  2. Verify Requests: Use Symfony’s Profiler or HttpClient middleware to inspect outgoing API calls:

    $client = new Client([
        'headers' => ['X-Adback-Key' => $this->apiKey],
    ]);
    
  3. Test Locally: Mock the AdbackApiClient in tests:

    $mock = $this->createMock(AdbackApiClient::class);
    $mock->method('isAdBlocked')->willReturn(true);
    $this->container->set(AdbackApiClient::class, $mock);
    

Extension Points

  1. 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');
        }
    }
    
  2. 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'];
    }
    
  3. 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);
        }
    }
    
  4. Database Integration: Store ad-block status in the database for analytics:

    public function logAd
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium