Installation:
composer require baconmanager/rd-station-bundle
Register Bundle:
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
Bacon\Bundle\CoreBundle\BaconRDStationBundle::class => ['all' => true],
Configure API Credentials:
Add to config/packages/bacon_rd_station.yaml (Symfony 4+) or config.yml:
bacon_rd_station:
api:
private_token: "%env(RD_STATION_PRIVATE_TOKEN)%"
token: "%env(RD_STATION_TOKEN)%"
Store tokens in .env for security.
First Use Case: Inject the service and test a lead creation:
use Bacon\Bundle\CoreBundle\Service\RDStationAPI;
class LeadController extends AbstractController
{
public function createLead(RDStationAPI $rdStation): JsonResponse
{
$response = $rdStation->api('conversions', 'POST', [
'email' => 'user@example.com',
'nome' => 'Test Lead'
]);
return $this->json($response);
}
}
Lead Management:
api('conversions', 'POST/PUT', $leadData) for lead lifecycle management.
$leadData = [
'email' => 'user@example.com',
'nome' => 'John Doe',
'phone' => '1234567890',
'tags' => ['prospect', 'high-value']
];
$rdStation->api('conversions', 'POST', $leadData);
api('conversions', 'GET', ['email' => 'user@example.com']) for retrieval.Event Tracking:
$rdStation->api('events', 'POST', [
'leadId' => 123,
'event' => 'page_view',
'url' => '/contact'
]);
Batch Operations:
api('conversions', 'POST', $leadsArray) to send multiple leads in a single call (if supported by RD Station API).RDStationAPI in controllers/services:
public function __construct(private RDStationAPI $rdStation) {}
try {
$response = $rdStation->api('conversions', 'POST', $data);
} catch (\Exception $e) {
$this->addFlash('error', 'RD Station API Error: ' . $e->getMessage());
}
config/packages/bacon_rd_station.yaml for environment-specific settings (e.g., sandbox vs. production tokens):
bacon_rd_station:
api:
private_token: "%env(RD_STATION_SANDBOX_TOKEN)%"
token: "%env(RD_STATION_SANDBOX_TOKEN)%"
sandbox: true # Add if the bundle supports sandbox mode
Token Security:
.env and %env().$rdStation->api('conversions', 'GET', ['email' => 'test@example.com']);
Check for 401 Unauthorized responses.Rate Limiting:
$attempts = 0;
$maxAttempts = 3;
while ($attempts < $maxAttempts) {
try {
$response = $rdStation->api('conversions', 'POST', $data);
break;
} catch (\Exception $e) {
$attempts++;
if ($attempts === $maxAttempts) throw $e;
sleep(2 ** $attempts); // Exponential backoff
}
}
Data Validation:
$requiredFields = ['email', 'nome'];
foreach ($requiredFields as $field) {
if (empty($data[$field])) {
throw new \InvalidArgumentException("Missing required field: $field");
}
}
Deprecation:
Enable API Debugging:
Add a debug flag to config/packages/bacon_rd_station.yaml:
bacon_rd_station:
debug: true
Check logs for raw API responses (if the bundle supports it).
Common Errors:
nome must be a string).conversions vs. leads).Custom Endpoints: Extend the bundle by creating a decorator service:
# config/services.yaml
Bacon\Bundle\CoreBundle\Service\RDStationAPI:
decorates: 'bacon_rd_station.api'
arguments: ['@bacon_rd_station.api.decorated']
Implement custom logic in the decorator (e.g., logging, transformation).
Event Listeners: Subscribe to bundle events (if available) to intercept API calls:
// src/EventListener/RDStationListener.php
public static function getSubscribedEvents()
{
return [
'bacon_rd_station.api.call' => 'onApiCall',
];
}
Testing:
Mock the RDStationAPI service in PHPUnit:
$this->createMock(RDStationAPI::class)
->method('api')
->willReturn(['success' => true]);
How can I help you explore Laravel packages today?