Installation:
composer require derkien/eight-bit-bundle:1.*
Ensure SensioBuzzBundle is enabled in AppKernel.php (as shown in the README).
Configure the Bundle:
Define a client service in app/config/config.yml:
eight_bit:
clients:
my_client:
url: "https://api.example.com/locations"
auth: { token: "your_api_token" } # Optional auth config
First Use Case: Fetch and display locations via the default controller:
// src/AppBundle/Controller/LocationController.php
use Derkien\EightBitBundle\Controller\LocationController;
class LocationController extends LocationController
{
public function indexAction()
{
$locations = $this->get('eight_bit.client.my_client')->fetch();
return $this->render('AppBundle:Location:index.html.twig', ['locations' => $locations]);
}
}
Access via /location (if routing is configured).
Service-Based Client Configuration:
config.yml under eight_bit.clients with keys like url, auth, and options (e.g., timeout).$client = $this->get('eight_bit.client.my_client');
$locations = $client->fetch(); // Returns array of `EightBitBundle\Entity\Location`
Error Handling:
EightBitBundle\Exception\CurlException).EightBitBundle\Exception\JsonException).EightBitBundle\Exception\ApiException).try {
$locations = $client->fetch();
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
return $this->redirect($this->generateUrl('home'));
}
Customizing the Entity:
EightBitBundle\Entity\Location to add custom properties/methods:
namespace AppBundle\Entity;
use Derkien\EightBitBundle\Entity\Location as BaseLocation;
class Location extends BaseLocation
{
public function getFormattedAddress()
{
return $this->street . ', ' . $this->city;
}
}
services.yml:
eight_bit.entity.location.class: AppBundle\Entity\Location
Reusing Logic:
// src/AppBundle/Service/LocationService.php
class LocationService
{
private $client;
public function __construct($client)
{
$this->client = $client;
}
public function getNearbyLocations($lat, $lng)
{
$this->client->setOptions(['query' => ['lat' => $lat, 'lng' => $lng]]);
return $this->client->fetch();
}
}
services.yml:
app.location_service:
class: AppBundle\Service\LocationService
arguments: ['@eight_bit.client.my_client']
Caching Responses:
$cache = $this->get('cache.app');
$cacheKey = 'locations_' . md5($client->getUrl());
if (!$locations = $cache->get($cacheKey)) {
$locations = $client->fetch();
$cache->set($cacheKey, $locations, 3600); // Cache for 1 hour
}
Deprecated Dependencies:
sensio/buzz-bundle is outdated; consider replacing it with php-http/guzzle-bundle for modern projects.Entity Mapping Assumptions:
EightBitBundle\Entity\Location. If the API returns a nested array (e.g., {"data": [...]}), override the fetch() method in a custom client class:
use Derkien\EightBitBundle\Client\Client;
class CustomClient extends Client
{
protected function parseResponse($response)
{
return parent::parseResponse($response['data']); // Adjust as needed
}
}
CURL Configuration:
eight_bit:
clients:
my_client:
url: "https://api.example.com/locations"
options:
headers: { "X-API-Key": "your_key" }
timeout: 10
Routing Conflicts:
derkien-eight-bit) may conflict with existing routes. Either:
EightBitBundle/Resources/config/routing.yml.derkien_eight_bit_homepage:
path: /custom-path
defaults: { _controller: EightBitBundle:Location:index }
No Rate Limiting:
// src/AppBundle/EventListener/RateLimitListener.php
class RateLimitListener
{
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->getRequest()->attributes->get('_route') === 'derkien_eight_bit') {
// Add rate limiting logic (e.g., using `stof/doctrine-extensions-bundle`)
}
}
}
Enable CURL Debugging:
Add to config.yml to log CURL requests:
eight_bit:
clients:
my_client:
options:
debug: true
Check logs in var/log/dev.log.
Inspect Raw Responses:
Override the fetch() method to log raw responses:
$client->setLogger($this->get('logger')); // Inject Symfony logger
Validate JSON Structure: Use tools like JSONLint to verify API responses match the expected structure before debugging.
Custom Clients:
Extend Derkien\EightBitBundle\Client\Client to support additional features (e.g., pagination, webhooks):
class PaginatedClient extends Client
{
public function fetchPage($page = 1)
{
$this->setOptions(['page' => $page]);
return $this->fetch();
}
}
Event Dispatching: Trigger events before/after API calls using Symfony’s event system:
// In your custom client:
$dispatcher = $this->get('event_dispatcher');
$dispatcher->dispatch('eight_bit.fetch', new EightBitEvent($this));
Dynamic Configuration: Load client configurations dynamically (e.g., from a database) by overriding the bundle’s compiler pass:
// src/AppBundle/DependencyInjection/Compiler/DynamicEightBitPass.php
class DynamicEightBitPass extends CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$configs = $this->getDynamicConfigs(); // Fetch from DB/API
foreach ($configs as $name => $config) {
$container->setDefinition(
'eight_bit.client.' . $name,
new Definition('Derkien\EightBitBundle\Client\Client', [$config])
);
}
}
}
How can I help you explore Laravel packages today?