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

earlybirds/facebook-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require earlybirds/facebook-bundle:dev-master
    php composer.phar update
    

    Ensure friendsofsymfony/facebook-bundle and friendsofsymfony/user-bundle are also installed (as dependencies).

  2. Register the Bundle: Add to app/ApplicationKernel.php:

    new EB\FacebookBundle\EBFacebookBundle(),
    new FOS\FacebookBundle\FOSFacebookBundle(),
    new FOS\UserBundle\FOSUserBundle(),
    
  3. Routing: Include in app/config/routing.yml:

    eb_facebook:
        resource: "@EBFacebookBundle/Resources/config/routing.xml"
    
  4. Configuration: Add to app/config/config.yml:

    eb_facebook:
        app_id: YOUR_APP_ID
        secret: YOUR_APP_SECRET
        templates:
            layout: YourBundle::layout.html.twig
    
  5. First Use Case: Trigger the Facebook login flow by visiting /login/facebook (default route). The bundle handles OAuth, redirects, and user creation via FOSUserBundle.


Implementation Patterns

Core Workflows

  1. Authentication Flow:

    • Use the /login/facebook route to initiate OAuth.
    • The bundle redirects users to Facebook, handles the callback at /login/check-facebook, and creates a FOSUserBundle user if authenticated.
    • Customize user creation by overriding EBFacebookBundle:User:connect controller.
  2. Post-Auth Actions:

    • After login, redirect users to a custom route (e.g., /dashboard) by configuring post_login_redirect in config.yml:
      eb_facebook:
          post_login_redirect: /dashboard
      
  3. Facebook API Integration:

    • Access the Facebook SDK via the facebook service (injected by FOSFacebookBundle):
      $facebook = $this->get('facebook');
      $me = $facebook->getUser(); // Get logged-in user's Facebook ID
      $userData = $facebook->api('/me'); // Fetch user data
      
    • Use in controllers or services to fetch user profiles, posts, or publish actions.
  4. Templating:

    • Extend the default layout (layout.html.twig) by overriding the layout template path in config.
    • Use Twig to render Facebook SDK JS or dynamic content:
      {{ render(controller('EBFacebookBundle:Facebook:jsSdk')) }}
      
  5. Event Listeners:

    • Subscribe to facebook.connect (post-login) or facebook.login (pre-login) events to extend functionality:
      # app/config/services.yml
      services:
          my.facebook.listener:
              class: AppBundle\EventListener\FacebookListener
              tags:
                  - { name: kernel.event_listener, event: facebook.connect, method: onFacebookConnect }
      

Integration Tips

  • User Sync: Sync Facebook user data (e.g., email, name) with FOSUserBundle by extending the UserManager or overriding the connect action.

  • Permissions: Request extended permissions (e.g., email, user_birthday) by configuring the scope in config.yml:

    eb_facebook:
        scope: ["email", "public_profile"]
    
  • Error Handling: Catch Facebook API errors (e.g., expired tokens) in a global exception handler or listener.

  • Testing: Use the FacebookMock service (from FOSFacebookBundle) to mock API responses in PHPUnit tests:

    $this->get('facebook.mock')->setMockResponse(['id' => '123']);
    

Gotchas and Tips

Pitfalls

  1. Deprecated Dependencies:

    • The bundle relies on dev-master of friendsofsymfony/facebook-bundle and user-bundle, which may introduce instability. Pin versions explicitly in composer.json:
      "friendsofsymfony/facebook-bundle": "~3.0",
      "friendsofsymfony/user-bundle": "~2.0"
      
  2. Routing Conflicts:

    • Ensure /login/facebook and /login/check-facebook routes don’t clash with existing routes. Use _route in Twig to generate URLs:
      {{ path('_eb_facebook_login') }}
      
  3. CSRF Issues:

    • Facebook OAuth callbacks may fail if CSRF protection is enabled. Exclude the callback route in security.yml:
      security:
          access_control:
              - { path: ^/login/check-facebook, roles: IS_AUTHENTICATED_ANONYMOUSLY }
      
  4. User Creation:

    • If FOSUserBundle is misconfigured, Facebook logins may silently fail. Verify the User class extends FOS\UserBundle\Model\User and the UserManager is properly set up.
  5. Template Overrides:

    • Overriding templates (e.g., layout.html.twig) requires the bundle’s template paths to be correctly resolved. Use debug:config to verify:
      php bin/console debug:config eb_facebook
      

Debugging Tips

  1. Enable Facebook SDK Logging: Add to config.yml:

    fos_facebook:
        logging: true
    

    Check logs for OAuth errors or API responses.

  2. Validate App ID/Secret: Test the Facebook App ID and Secret in the Facebook Graph API Explorer.

  3. Token Debugging: Dump the access token after login to verify scope:

    $token = $this->get('facebook')->getAccessToken();
    var_dump($token);
    
  4. Route Debugging: List all routes to confirm Facebook routes are registered:

    php bin/console debug:router | grep eb_facebook
    

Extension Points

  1. Custom User Provider: Override the default user provider to fetch additional Facebook data:

    // src/AppBundle/Provider/FacebookUserProvider.php
    class FacebookUserProvider extends \EB\FacebookBundle\Provider\FacebookUserProvider
    {
        public function loadUserByFacebookId($facebookId)
        {
            $data = $this->getFacebook()->api('/' . $facebookId);
            // Custom logic to create/update user
        }
    }
    

    Register as a service and set in config.yml:

    eb_facebook:
        user_provider: app.facebook_user_provider
    
  2. Post-Login Redirects: Dynamically set redirects based on user roles or data:

    eb_facebook:
        post_login_redirect: app.facebook_redirect_handler
    

    Implement the handler as a service:

    class FacebookRedirectHandler
    {
        public function getRedirectUrl()
        {
            return '/dashboard'; // Logic here
        }
    }
    
  3. Custom Templates: Override any template in EBFacebookBundle by creating a matching path in your bundle (e.g., AcmeBundle/Resources/views/EBFacebookBundle/Layout/layout.html.twig).

  4. Event Subscribers: Extend the bundle’s behavior via events (e.g., facebook.connect):

    class MyFacebookSubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return ['facebook.connect' => 'onFacebookConnect'];
        }
    
        public function onFacebookConnect(FacebookEvent $event)
        {
            $user = $event->getUser();
            // Custom logic
        }
    }
    
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui