Installation
composer require friendsofsymfony/facebook-bundle
(Note: Despite being deprecated, this bundle remains functional for legacy projects.)
Configuration
Add to config/packages/fos_facebook.yaml (Symfony 4+):
fos_facebook:
app_id: '%env(FACEBOOK_APP_ID)%'
secret: '%env(FACEBOOK_APP_SECRET)%'
scope: ['email', 'public_profile']
graph_api_version: 'v12.0'
First Use Case: Login Button
Add a route to config/routes.yaml:
fos_facebook_connect_connect: ~
fos_facebook_connect_check: ~
Render the login button in a Twig template:
{{ render(controller('FOSFacebookBundle:Connect:connect', {'service_id': 'facebook'})) }}
Service Configuration
Extend fos_facebook.yaml to define custom user providers:
fos_facebook:
service:
user_provider_id: 'app.user_provider.facebook'
Custom User Provider
Implement UserProviderInterface to map Facebook data to your user model:
// src/User/FacebookUserProvider.php
class FacebookUserProvider implements UserProviderInterface
{
public function loadUserByFacebookId($facebookId)
{
return User::where('facebook_id', $facebookId)->first();
}
}
Post-Login Logic
Use Symfony’s AuthenticationSuccessHandler to redirect or fetch additional data:
// src/EventListener/FacebookLoginListener.php
class FacebookLoginListener
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
if ($providerKey === 'facebook') {
$user = $token->getUser();
$facebookData = $this->facebookManager->getUserData();
// Merge or update user data
}
}
}
Graph API Calls
Use the built-in FacebookManager to fetch user data:
$userData = $this->facebookManager->getUserData();
// Access fields like $userData['email'], $userData['name']
Permissions & Scopes Extend scopes dynamically in runtime:
fos_facebook:
scope: ['email', 'public_profile', '%env(FACEBOOK_EXTRA_SCOPES)%']
Deprecation Warnings
hwi/oauth-bundle for new projects.Graph API Versioning
graph_api_version (e.g., v12.0). Facebook breaks backward compatibility frequently.Token Expiry & Refresh
offline_access scope (if available) for long-lived tokens.TokenRefreshListener to handle token expiry gracefully.CSRF & Security
secret in fos_facebook.yaml matches your Facebook App settings.Enable Debug Mode
Set debug: true in fos_facebook.yaml to log API errors:
fos_facebook:
debug: '%kernel.debug%'
Facebook SDK Logs
Check Symfony’s var/log/dev.log for Facebook SDK errors (e.g., invalid app ID/secret).
Common Errors & Fixes
| Error | Solution |
|---|---|
Invalid OAuth access token |
Regenerate the Facebook App token in App Dashboard. |
Missing required scope |
Add the missing scope to fos_facebook.scope. |
User not found |
Implement a fallback user creation logic in your UserProvider. |
Custom Fields
Extend the FacebookManager to fetch non-standard fields:
$customData = $this->facebookManager->get('/me?fields=custom_field', ['access_token' => $token]);
Webhook Integration Use Facebook’s Graph API Webhooks to handle real-time events (e.g., likes, comments) via a Symfony controller:
// src/Controller/FacebookWebhookController.php
class FacebookWebhookController extends AbstractController
{
public function verify(Request $request)
{
$hubVerifyToken = $request->query->get('hub_verify_token');
// Verify with Facebook and return challenge
}
public function handle(Request $request)
{
$data = json_decode($request->getContent(), true);
// Process webhook payload
}
}
Multi-Provider Setup
Combine with other OAuth providers (e.g., Google) by configuring multiple services in fos_facebook.yaml:
fos_facebook:
service:
facebook:
app_id: '%env(FACEBOOK_APP_ID)%'
secret: '%env(FACEBOOK_APP_SECRET)%'
google:
app_id: '%env(GOOGLE_CLIENT_ID)%'
secret: '%env(GOOGLE_CLIENT_SECRET)%'
How can I help you explore Laravel packages today?