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

Sape Bundle Laravel Package

anh/sape-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Bundle Run:

    composer require anh/sape-bundle:~2.0
    

    Ensure anh/sape-php-client (dependency) is also installed.

  2. Enable the Bundle Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony <4):

    Anh\SapeBundle\AnhSapeBundle::class => ['all' => true],
    
  3. Configure SAPE Add your credentials to config/packages/anh_sape.yaml (Symfony 4+) or config.yml (Symfony <4):

    anh_sape:
        user: 'your_sape_user_id_here'  # e.g., '80e20e4a72a1d09895763b4b1bc98e63'
        client_options: { charset: 'utf8' }  # Optional; defaults to utf8
    
  4. First Use Case Generate SAPE links in Twig:

    {% set links = sape_return_links() %}
    {% for link in links %}
        <a href="{{ link.url }}">{{ link.title }}</a>
    {% endfor %}
    

Implementation Patterns

Core Workflows

  1. Dynamic Link Generation

    • Use sape_return_links() in Twig to fetch and render SAPE links for a page.
    • Cache results if performance is critical (e.g., via Symfony Cache component):
      {% set cachedLinks = cache(app.cache, 'sape_links', 3600) %}
      {% if not cachedLinks %}
          {% set cachedLinks = sape_return_links() %}
          {% do cache(app.cache, 'sape_links', cachedLinks, 3600) %}
      {% endif %}
      
  2. Block-Specific Links

    • Use sape_return_block_links() to target specific content blocks (e.g., articles, products):
      {{ sape_return_block_links(blockId) }}
      
  3. Controller Integration

    • Pass SAPE data to templates via controller:
      // src/Controller/PageController.php
      public function show(Page $page): Response
      {
          $sapeLinks = $this->get('anh_sape.client')->getLinks();
          return $this->render('page/show.html.twig', [
              'links' => $sapeLinks,
          ]);
      }
      
  4. Event-Driven Updates

    • Trigger SAPE link updates post-content changes (e.g., using Symfony Events):
      // src/EventListener/SapeUpdateListener.php
      public function onContentUpdate(ContentEvent $event)
      {
          $this->get('anh_sape.client')->updateLinks();
      }
      

Integration Tips

  • Symfony Forms: Attach SAPE links to form fields for ad revenue:
    {{ form_row(form.field) }}
    <div class="sape-links">
        {{ sape_return_block_links('form_field_' ~ form.vars.id) }}
    </div>
    
  • API Endpoints: Expose SAPE links via API for SPAs:
    // src/Controller/SapeApiController.php
    public function getLinks(): JsonResponse
    {
        return $this->json($this->get('anh_sape.client')->getLinks());
    }
    
  • Twig Extensions: Extend the bundle’s Twig functions:
    // src/Twig/SapeExtension.php
    public function getFilteredLinks(array $filters): array
    {
        return $this->client->getLinks()->filter($filters);
    }
    

Gotchas and Tips

Pitfalls

  1. User ID Validation

    • SAPE requires a valid user ID in config. Test with:
      php bin/console debug:config anh_sape
      
    • Invalid IDs return empty arrays; debug with:
      $client = $this->get('anh_sape.client');
      var_dump($client->getLinks()); // Should not be empty
      
  2. Character Encoding

    • Default charset: utf8 may fail for non-UTF8 pages. Override in config:
      client_options: { charset: 'windows-1251' }
      
  3. Caching Conflicts

    • SAPE links may change frequently. Avoid aggressive caching:
      # config/packages/cache.yaml
      app.cache.sape:
          adapter: cache.adapter.apcu
          provider: 'cache.provider.doctrine'
          default_lifetime: 300 # 5 minutes
      
  4. Twig Function Scope

    • sape_return_links() is Twig-only. For PHP, use:
      $links = $this->get('anh_sape.client')->getLinks();
      

Debugging

  • Log SAPE Responses Enable debug mode in config/packages/dev/anh_sape.yaml:

    anh_sape:
        debug: true
    

    Check logs for raw SAPE responses:

    grep -i "sape" var/log/dev.log
    
  • Test Locally Use a local SAPE sandbox user ID to avoid live API calls during development.

Extension Points

  1. Custom Link Filters Extend the client to filter links by type/language:

    // src/Service/SapeClient.php
    public function getLinksByType(string $type): array
    {
        return array_filter($this->getLinks(), fn($link) => $link->type === $type);
    }
    
  2. Async Processing Offload SAPE updates to a queue (e.g., Symfony Messenger):

    // src/Message/SapeUpdateMessage.php
    public function __construct(private string $contentId) {}
    
  3. Multi-Account Support Override the bundle’s SAPE_client to support multiple SAPE accounts:

    // src/DependencyInjection/AnhSapeExtension.php
    public function load(array $configs, ContainerBuilder $container)
    {
        $config = $this->processConfiguration($configuration, $configs);
        $container->setParameter('anh_sape.users', $config['users']);
    }
    
  4. Webhook Integration Listen for SAPE link updates via webhooks:

    // src/EventListener/SapeWebhookListener.php
    public function onWebhook(Request $request)
    {
        $data = json_decode($request->getContent(), true);
        $this->get('anh_sape.client')->updateFromWebhook($data);
    }
    
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.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony