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],
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.
Declarative Integration:
fb-like, twitter-share-button) and loads the SDK dynamically.resources/views/partials/share-buttons.blade.php):
@if($showFacebook)
<div class="fb-like" data-href="{{ $url }}"></div>
@endif
Conditional Loading:
// config/social_sdk.php
'excluded_routes' => ['admin.*'],
Customization:
// config/social_sdk.php
'facebook' => [
'app_id' => env('FACEBOOK_APP_ID', 'your_app_id'),
'version' => 'v12.0',
],
Twig/Laravel Blade Integration:
// Laravel Blade
@socialSdkButton('facebook', ['href' => url('/post')])
Asset Management:
<body> via Twig’s {% block javascripts %} or Laravel’s @stack('scripts'):
@stack('scripts')
<!-- Bundle injects SDKs here if needed -->
Route Exclusions:
excluded_routes in config/social_sdk.php:
'excluded_routes' => ['admin.*', 'api.*'], // Regex or glob patterns
Button Selectors:
<!-- Non-standard button? -->
<button class="custom-twitter-share" data-url="{{ url('/') }}"></button>
Fix: Extend SdkBase to override detection logic.Caching:
data-sdktime attributes to bust caches:
// config/social_sdk.php
'cache_busting' => true,
Asynchronous Loading:
public function getScriptTag(): string
{
return '<script async src="..."></script>';
}
Debugging:
Network tab). Missing SDKs often indicate:
Extending for New SDKs:
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
);
}
}
config/social_sdk.php:
'providers' => [
'linkedin' => App\SocialSDK\LinkedInSdk::class,
],
Performance:
// In a service provider
$this->app['social_sdk']->forceSdk('facebook');
Testing:
$this->partialMock(SocialSDKManager::class, ['detectButtons'])
->method('detectButtons')
->willReturn(['facebook' => true]);
Configuration:
// config/social_sdk.php
'facebook' => [
'app_id' => env('SOCIAL_SDK_FACEBOOK_APP_ID'),
],
SOCIAL_SDK_FACEBOOK_APP_ID=your_app_id_here
Fallbacks:
@if(!socialSdkLoaded('facebook'))
<script src="https://connect.facebook.net/en_US/sdk.js"></script>
@endif
How can I help you explore Laravel packages today?