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

Facebook Bundle Laravel Package

core23/facebook-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require core23/facebook-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        Core23\FacebookBundle\Core23FacebookBundle::class => ['all' => true],
    ];
    
  2. 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'
    
  3. 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);
        }
    }
    

Implementation Patterns

Common Workflows

  1. 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']);
    
  2. API Integration: Fetch data with pagination:

    $posts = $this->facebook->get('/me/feed', ['limit' => 5]);
    $nextPage = $posts->getNextPageUrl();
    
  3. 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]);
        }
    }
    
  4. 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
    }
    

Integration Tips

  • Caching: Cache API responses using Symfony’s cache system:
    $response = $this->facebook->get('/me', [], 3600); // Cache for 1 hour
    
  • Error Handling: Wrap API calls in try-catch:
    try {
        $response = $this->facebook->get('/invalid_endpoint');
    } catch (\Facebook\Exceptions\FacebookResponseException $e) {
        // Handle HTTP errors
    } catch (\Facebook\Exceptions\FacebookSDKException $e) {
        // Handle SDK errors
    }
    
  • Environment-Specific Config: Use %kernel.environment% in core23_facebook.yaml for dev/staging/prod settings.

Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • The bundle is archived (last release in 2020) and lacks active maintenance. Use at your own risk.
    • Consider alternatives like facebook/graph-sdk for newer Laravel/Symfony projects.
  2. Sonata Dependency:

    • The bundle assumes SonataAdminBundle is installed. If not, the FacebookBlockService will fail to initialize.
    • Workaround: Manually instantiate FacebookService and use it directly.
  3. Graph API Version:

    • Hardcoding v12.0 may break in the future. Monitor Facebook’s API deprecations.
  4. State Parameter:

    • Always include a state parameter in OAuth login URLs to prevent CSRF:
      $authUrl = $this->facebook->getLoginUrl(['email'], ['state' => 'random_string']);
      

Debugging

  • Enable Debug Mode: Set debug: true in core23_facebook.yaml to log raw API responses:
    core23_facebook:
        debug: true
    
  • Check Headers: Inspect the Authorization header in failed requests using:
    $this->facebook->getClient()->setDefaultCurlOptions([
        CURLOPT_VERBOSE => true,
    ]);
    

Extension Points

  1. 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']
    
  2. Override Webhook Validation: Extend the FacebookWebhookValidator to add custom logic:

    class CustomWebhookValidator extends FacebookWebhookValidator
    {
        protected function validatePayload($payload)
        {
            // Custom validation
            return parent::validatePayload($payload);
        }
    }
    
  3. Logging: Replace the default logger with Monolog:

    core23_facebook:
        logger: '@monolog.logger.facebook'
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware