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

Spotify Web Api Bundle Laravel Package

calliostro/spotify-web-api-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Installation: Require the package via Composer:
    composer require calliostro/spotify-web-api-bundle
    
  2. Configuration: Update config/packages/calliostro_spotify_web_api.yaml (new PHP format) with your Spotify API credentials:
    client_id: '%env(SPOTIFY_CLIENT_ID)%'
    client_secret: '%env(SPOTIFY_CLIENT_SECRET)%'
    redirect_uri: '%env(SPOTIFY_REDIRECT_URI)%'
    
  3. First Use Case: Authenticate and fetch user data:
    use Calliostro\SpotifyWebApiBundle\Service\SpotifyService;
    
    public function showProfile(SpotifyService $spotify)
    {
        $user = $spotify->getMe();
        return response()->json($user);
    }
    
    • Note: The bundle now auto-detects jwilsson/spotify-web-api-php v6 or v7 (no manual version selection required).

Implementation Patterns

Configuration

  • PHP Format: Replace deprecated services.xml with YAML/ENV-based config (e.g., .env or config/packages/).
    # config/packages/calliostro_spotify_web_api.yaml
    spotify:
        client_id: '%env(SPOTIFY_CLIENT_ID)%'
        client_secret: '%env(SPOTIFY_CLIENT_SECRET)%'
        cache_adapter: 'cache.app' # Optional: Use Symfony cache
    
  • Symfony Dependency Injection: Inject SpotifyService directly into controllers/services:
    public function __construct(private SpotifyService $spotify) {}
    

Common Workflows

  1. Authentication:
    $authUrl = $spotify->getAuthUrl(['scope' => ['user-read-private']]);
    // Redirect user to $authUrl, then handle callback with:
    $token = $spotify->handleAuthCallback($request);
    
  2. API Calls:
    // Tracks
    $tracks = $spotify->search('artist:Taylor Swift', ['type' => 'track']);
    // Playlists
    $playlists = $spotify->getUserPlaylists('user-id');
    
  3. Caching: Leverage Symfony’s cache system (configured via cache_adapter):
    $spotify->setCache($cacheItemPool); // Manually override if needed
    

Integration Tips

  • Laravel Mixins: Extend the SpotifyService for custom logic:
    $spotify->extend(function ($spotify) {
        $spotify->getCustomData = function () {
            return $this->getMe()->toArray() + ['custom_field' => true];
        };
    });
    
  • Event Listeners: Use Symfony events for pre/post API calls:
    // config/services.yaml
    services:
        App\Listener\SpotifyLogger:
            tags:
                - { name: kernel.event_listener, event: spotify.api.call, method: onApiCall }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Config:
    • Error: services.xml no longer exists. Use YAML/ENV instead.
    • Fix: Run php bin/console debug:config calliostro_spotify_web_api to validate config.
  2. Spotify API Version:
    • Gotcha: The bundle auto-selects v6/v7, but ensure your jwilsson/spotify-web-api-php dependency is updated:
      composer require jwilsson/spotify-web-api-php:^7.0
      
    • Debug: Check version with:
      $spotify->getClient()->getVersion();
      
  3. Cache Issues:
    • Symptom: Stale data if cache is misconfigured.
    • Solution: Clear cache after config changes:
      php bin/console cache:clear
      

Debugging

  • Enable Debug Mode:
    # config/packages/calliostro_spotify_web_api.yaml
    debug: '%kernel.debug%'
    
  • Log API Calls:
    $spotify->setLogger(new \Monolog\Logger('spotify', [...]));
    

Extension Points

  1. Custom Clients: Override the default SpotifyClient:
    $spotify->setClient(new \SpotifyWebAPI\SpotifyWebAPI([
        'client_id' => 'custom-id',
        'client_secret' => 'custom-secret',
    ]));
    
  2. Middleware: Add HTTP middleware for request/response modification:
    $spotify->setMiddleware(new class implements \SpotifyWebAPI\Middleware {
        public function handle($request, callable $next) { ... }
    });
    
  3. Testing: Use the SpotifyService mock in PHPUnit:
    $mock = $this->createMock(\SpotifyWebAPI\SpotifyWebAPI::class);
    $spotify = new SpotifyService($mock);
    

Symfony-Specific Tips

  • Environment Variables: Prefix Spotify keys in .env:
    SPOTIFY_CLIENT_ID=your_id_here
    SPOTIFY_CLIENT_SECRET=your_secret_here
    
  • Container Awareness: Access the bundle’s container-configured services:
    $container->get('calliostro_spotify_web_api.service');
    
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