Installation:
composer require core23/facebook-bundle
Add to config/bundles.php:
return [
// ...
Core23\FacebookBundle\Core23FacebookBundle::class => ['all' => true],
];
Configuration:
Add Facebook API credentials to config/packages/core23_facebook.yaml:
core23_facebook:
app_id: '%env(FACEBOOK_APP_ID)%'
app_secret: '%env(FACEBOOK_APP_SECRET)%'
default_graph_version: 'v12.0'
First Use Case:
Inject the FacebookService in a controller/service:
use Core23\FacebookBundle\Service\FacebookService;
class MyController extends AbstractController
{
public function __construct(private FacebookService $facebook)
{
}
public function fetchUserData()
{
$response = $this->facebook->get('/me?fields=id,name,email');
return json_decode($response->getDecodedBody(), true);
}
}
Authentication: Use the built-in OAuth flow for user login:
$authUrl = $this->facebook->getLoginUrl(['email', 'public_profile']);
// Redirect user to $authUrl
$userData = $this->facebook->getAccessTokenData($_GET['code']);
API Integration: Fetch data with pagination:
$posts = $this->facebook->get('/me/feed', ['limit' => 5]);
$nextPage = $posts->getNextPageUrl();
Sonata Admin Integration: Extend a Sonata admin block:
use Core23\FacebookBundle\Block\FacebookBlockService;
class MyAdminBlock extends AbstractBlockService
{
public function __construct(private FacebookBlockService $facebookBlock)
{
}
public function execute()
{
$data = $this->facebookBlock->get('/me');
return $this->render('MyAdminBundle:Block:facebook.html.twig', ['data' => $data]);
}
}
Webhook Handling: Validate and process webhooks in a controller:
public function handleWebhook(Request $request)
{
$challenge = $this->facebook->validateWebhook($request->getContent());
if ($challenge) {
return new Response($challenge);
}
// Process webhook payload
}
$response = $this->facebook->get('/me', [], 3600); // Cache for 1 hour
try {
$response = $this->facebook->get('/invalid_endpoint');
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
// Handle HTTP errors
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
// Handle SDK errors
}
%kernel.environment% in core23_facebook.yaml for dev/staging/prod settings.Deprecated Package:
facebook/graph-sdk for newer Laravel/Symfony projects.Sonata Dependency:
FacebookBlockService will fail to initialize.FacebookService and use it directly.Graph API Version:
v12.0 may break in the future. Monitor Facebook’s API deprecations.State Parameter:
state parameter in OAuth login URLs to prevent CSRF:
$authUrl = $this->facebook->getLoginUrl(['email'], ['state' => 'random_string']);
debug: true in core23_facebook.yaml to log raw API responses:
core23_facebook:
debug: true
Authorization header in failed requests using:
$this->facebook->getClient()->setDefaultCurlOptions([
CURLOPT_VERBOSE => true,
]);
Custom Graph Endpoints:
Extend the FacebookService to add project-specific methods:
class CustomFacebookService extends FacebookService
{
public function getCustomData()
{
return $this->get('/custom_endpoint', ['param' => 'value']);
}
}
Register as a service in services.yaml:
services:
App\Service\CustomFacebookService:
decorates: 'core23_facebook.service.facebook'
arguments: ['@core23_facebook.service.facebook']
Override Webhook Validation:
Extend the FacebookWebhookValidator to add custom logic:
class CustomWebhookValidator extends FacebookWebhookValidator
{
protected function validatePayload($payload)
{
// Custom validation
return parent::validatePayload($payload);
}
}
Logging: Replace the default logger with Monolog:
core23_facebook:
logger: '@monolog.logger.facebook'
How can I help you explore Laravel packages today?