Installation:
composer require astina/social-links-bundle:dev-master
Add to AppKernel.php:
new Astina\Bundle\SocialLinksBundle\AstinaSocialLinksBundle(),
First Use Case: Generate a Facebook share link in Twig:
{{ social_link('facebook') }}
Outputs a <a> tag with the current request URL pre-populated in Facebook’s share dialog.
social_link() in Twig templates.title, text, attributes, linkText).twitter, linkedin).{{ social_link('twitter') }} {# Uses request.getUri() #}
{{ social_link('pinterest', 'https://example.com/product') }}
{{ social_link('facebook', null, {'attributes': {'class': 'btn-share', 'data-tooltip': 'Share'}}) }}
{{ social_link('instagram', null, null, 'Share on IG') }}
app/Resources/AstinaSocialLinksBundle/views/SocialLinks/socialLink.html.twig to customize the <a> tag structure:
<button class="share-btn" onclick="window.open('{{ link.href }}', '_blank')">
{{ linkText|default(provider|title) }}
</button>
SocialLinks service in controllers:
use Astina\Bundle\SocialLinksBundle\SocialLinks\SocialLinks;
public function shareAction(SocialLinks $socialLinks)
{
$link = $socialLinks->getLink('linkedin', 'https://example.com');
return new Response($link->getHtml());
}
{% set providers = ['facebook', 'twitter', 'linkedin'] %}
{% for provider in providers %}
{{ social_link(provider) }}
{% endfor %}
Deprecated Bundle:
social-links/social-links:^2.0).URL Encoding Issues:
?, #) may not encode correctly. Manually encode URLs if needed:
{{ social_link('twitter', 'https://example.com/page#section') }}
{# May break; use urlencode_filter #}
Missing linkText:
Facebook). Override explicitly if needed:
{{ social_link('facebook', null, null, 'Custom Text') }}
Twig Extension Caching:
php bin/console cache:clear) if changes to the Twig extension aren’t reflected.Inspect Generated HTML:
href attribute matches expectations. Example:
<a href="https://www.facebook.com/sharer.php?u=https%3A%2F%2Fexample.com">
Check Provider Support:
Log SocialLinks Service:
$this->get('social_links')->getLink('twitter', 'https://example.com')->getUrl();
Add Custom Providers:
oscarotero/social-links by creating a custom provider class and registering it in the bundle’s configuration.Override Default Options:
AstinaSocialLinksBundle/Twig/SocialLinksExtension.php:
$options = array_merge([
'attributes' => ['target' => '_blank', 'rel' => 'noopener noreferrer'],
], $options);
Integrate with Forms:
$builder->add('share_link', HiddenType::class, [
'mapped' => false,
'data' => $this->get('social_links')->getLink('twitter', $entity->getUrl())->getHtml(),
]);
No Config File:
config.yml; all behavior is driven by Twig extension parameters.Request Context:
request.getUri(). For non-web contexts (e.g., CLI), pass an explicit URL:
{{ social_link('facebook', 'https://example.com') }}
Twig 3.x Compatibility:
{{ social_link(...) with {% set link = social_link(...) %}{{ link }} to avoid deprecated syntax.How can I help you explore Laravel packages today?