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

Social Sdk Bundle Laravel Package

alphalemon/social-sdk-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require alphalemon/social-sdk-bundle:dev-master
    

    If not using AlphaLemonBootstrapBundle, manually register the bundle in config/bundles.php (Laravel 5.4+) or AppKernel.php (older versions):

    AlphaLemon\Block\SocialSDKBundle\SocialSDKBundle::class => ['all' => true],
    
  2. First Use Case: Add a Facebook/Twitter button to your Blade template (e.g., resources/views/layouts/app.blade.php):

    <!-- Facebook -->
    <div class="fb-like" data-href="{{ url('/') }}" data-layout="button_count"></div>
    
    <!-- Twitter -->
    <a href="https://twitter.com/share" class="twitter-share-button" data-url="{{ url('/') }}">Tweet</a>
    

    The bundle will automatically inject the required SDK (<script> tags) at the end of the page only if the corresponding button exists.


Implementation Patterns

Core Workflow

  1. Declarative Integration:

    • No manual SDK inclusion in layouts. The bundle scans the DOM for social buttons (e.g., fb-like, twitter-share-button) and loads the SDK dynamically.
    • Example: Add a button in a partial view (e.g., resources/views/partials/share-buttons.blade.php):
      @if($showFacebook)
          <div class="fb-like" data-href="{{ $url }}"></div>
      @endif
      
  2. Conditional Loading:

    • SDKs are lazy-loaded (only when buttons are present). Optimize page weight by excluding SDKs from pages without social features.
    • Example: Disable SDK for a specific route:
      // config/social_sdk.php
      'excluded_routes' => ['admin.*'],
      
  3. Customization:

    • Override default SDK attributes (e.g., Facebook app ID) via config:
      // config/social_sdk.php
      'facebook' => [
          'app_id' => env('FACEBOOK_APP_ID', 'your_app_id'),
          'version' => 'v12.0',
      ],
      
  4. Twig/Laravel Blade Integration:

    • Use helpers to conditionally render buttons:
      // Laravel Blade
      @socialSdkButton('facebook', ['href' => url('/post')])
      
    • The helper checks for SDK requirements and renders the button.
  5. Asset Management:

    • SDK scripts are added to the end of the <body> via Twig’s {% block javascripts %} or Laravel’s @stack('scripts'):
      @stack('scripts')
      <!-- Bundle injects SDKs here if needed -->
      

Gotchas and Tips

Pitfalls

  1. Route Exclusions:

    • SDKs may load unexpectedly if excluded routes are misconfigured. Verify excluded_routes in config/social_sdk.php:
      'excluded_routes' => ['admin.*', 'api.*'], // Regex or glob patterns
      
  2. Button Selectors:

    • The bundle uses CSS selectors to detect buttons. Custom selectors require extending the provider (see below). Example:
      <!-- Non-standard button? -->
      <button class="custom-twitter-share" data-url="{{ url('/') }}"></button>
      
      Fix: Extend SdkBase to override detection logic.
  3. Caching:

    • SDK scripts may be cached aggressively by CDNs (e.g., Facebook). Use data-sdktime attributes to bust caches:
      // config/social_sdk.php
      'cache_busting' => true,
      
  4. Asynchronous Loading:

    • SDKs are loaded synchronously by default. For async loading, modify the provider:
      public function getScriptTag(): string
      {
          return '<script async src="..."></script>';
      }
      
  5. Debugging:

    • Check if SDKs are loaded by inspecting the page source or using browser dev tools (Network tab). Missing SDKs often indicate:
      • Incorrect button classes/attributes.
      • Route exclusions blocking the page.
      • Provider not registered (for custom SDKs).

Tips

  1. Extending for New SDKs:

    • Create a custom provider (e.g., LinkedInSdk) extending SdkBase:
      namespace App\SocialSDK;
      
      use AlphaLemon\Block\SocialSDKBundle\Core\Sdk\SdkBase;
      
      class LinkedInSdk extends SdkBase
      {
          protected $name = 'linkedin';
          protected $scriptUrl = 'https://platform.linkedin.com/batch';
          protected $buttonSelector = '.linkedin-share-button';
      
          public function getScriptTag(): string
          {
              return sprintf(
                  '<script type="in/batch" src="%s"></script>',
                  $this->scriptUrl
              );
          }
      }
      
    • Register the provider in config/social_sdk.php:
      'providers' => [
          'linkedin' => App\SocialSDK\LinkedInSdk::class,
      ],
      
  2. Performance:

    • Preload SDKs for critical pages (e.g., homepage) by forcing inclusion:
      // In a service provider
      $this->app['social_sdk']->forceSdk('facebook');
      
  3. Testing:

    • Mock SDK detection in PHPUnit:
      $this->partialMock(SocialSDKManager::class, ['detectButtons'])
           ->method('detectButtons')
           ->willReturn(['facebook' => true]);
      
  4. Configuration:

    • Override default config via environment variables:
      // config/social_sdk.php
      'facebook' => [
          'app_id' => env('SOCIAL_SDK_FACEBOOK_APP_ID'),
      ],
      
      SOCIAL_SDK_FACEBOOK_APP_ID=your_app_id_here
      
  5. Fallbacks:

    • Provide fallback scripts if the bundle fails:
      @if(!socialSdkLoaded('facebook'))
          <script src="https://connect.facebook.net/en_US/sdk.js"></script>
      @endif
      
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