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

Symfony Wp Bundle Laravel Package

blacktrs/symfony-wp-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle to your Laravel/Symfony project via Composer:

    composer require blacktrs/symfony-wp-bundle
    

    Register it in config/bundles.php (Symfony) or config/app.php (Laravel) if using a Symfony-based Laravel app:

    Blacktrs\SymfonyWpBundle\BlacktrsSymfonyWpBundle::class => ['all' => true],
    
  2. Publish Config Publish the default configuration:

    php artisan vendor:publish --tag=symfony-wp-config
    

    This generates config/symfony_wp.php with default settings for WordPress integration.

  3. First Use Case: Basic WordPress API Access Use the bundle’s service to fetch WordPress data (e.g., posts, users) via Symfony’s HTTP client or Laravel’s HTTP facade:

    use Blacktrs\SymfonyWpBundle\Service\WordPressService;
    
    $wpService = app(WordPressService::class);
    $posts = $wpService->getPosts(['per_page' => 5]);
    

    Ensure your .env has WordPress REST API credentials:

    WP_REST_URL=http://your-wordpress-site/wp-json
    WP_REST_USERNAME=admin
    WP_REST_PASSWORD=your_password
    

Implementation Patterns

1. Service Integration

  • Dependency Injection: Inject WordPressService into controllers/services:
    public function __construct(private WordPressService $wpService) {}
    
  • Laravel Facade (Optional): Create a facade for convenience:
    // app/Facades/WpFacade.php
    namespace App\Facades;
    use Blacktrs\SymfonyWpBundle\Service\WordPressService;
    use Illuminate\Support\Facades\Facade;
    
    class WpFacade extends Facade {
        protected static function getFacadeAccessor() { return WordPressService::class; }
    }
    
    Usage:
    $posts = \App\Facades\WpFacade::getPosts();
    

2. Caching Responses

  • Leverage Symfony’s HTTP cache or Laravel’s cache to avoid repeated API calls:
    $posts = $wpService->getPosts(['per_page' => 5], 300); // Cache for 5 minutes
    
    Configure cache drivers in config/symfony_wp.php:
    'cache' => [
        'enabled' => true,
        'driver' => 'file', // or 'redis', 'database'
        'ttl' => 300,
    ],
    

3. Event-Driven Workflows

  • Subscribe to WordPress webhooks (e.g., post updates) using Symfony’s Messenger or Laravel’s Events:
    // Example: Listen for post updates
    $wpService->onPostUpdated(function ($post) {
        // Sync with your app's database
    });
    
    Configure webhook endpoints in config/symfony_wp.php:
    'webhooks' => [
        'post_updated' => '/wp-webhook/post-updated',
    ],
    

4. Data Transformation

  • Use Laravel’s resource classes or Symfony’s serializers to transform WordPress data:
    // Example: Convert WP post to a custom resource
    $resource = new PostResource($wpService->getPost(123));
    return response()->json($resource);
    

5. Authentication

  • Support JWT/OAuth for secure API calls:
    $wpService->setAuth('jwt', [
        'token' => 'your_jwt_token',
        'expires_at' => now()->addHours(1),
    ]);
    

Gotchas and Tips

Pitfalls

  1. API Rate Limiting

    • WordPress REST API may throttle requests. Use caching aggressively and implement exponential backoff for retries:
      $wpService->setRetryOptions(['max_attempts' => 3, 'delay' => 100]);
      
  2. CORS Issues

    • If calling WordPress from a frontend, ensure CORS headers are configured in WordPress (wp-config.php):
      define('WP_ALLOW_CORS', true);
      
  3. Deprecated Endpoints

  4. Symfony vs. Laravel Quirks

    • If using Laravel, some Symfony-specific features (e.g., HttpClient middleware) may require manual setup. Prefer Laravel’s Http facade for consistency.

Debugging

  • Enable verbose logging in config/symfony_wp.php:
    'debug' => [
        'enabled' => true,
        'log_requests' => true,
    ],
    
  • Check logs at storage/logs/laravel.log for failed requests.

Extension Points

  1. Custom Endpoints Extend the service to support non-standard WordPress routes:

    // app/Services/ExtendedWordPressService.php
    class ExtendedWordPressService extends WordPressService {
        public function getCustomRoute($endpoint, $params = []) {
            return $this->httpClient->get($this->wpRestUrl . $endpoint, $params);
        }
    }
    
  2. Middleware Add request/response middleware for logging, auth, or transformation:

    // config/symfony_wp.php
    'middleware' => [
        \App\Http\Middleware\LogWpRequests::class,
    ],
    
  3. Testing Mock the WordPressService in PHPUnit:

    $mock = Mockery::mock(WordPressService::class);
    $mock->shouldReceive('getPosts')->andReturn([...]);
    $this->app->instance(WordPressService::class, $mock);
    

Pro Tips

  • Batch Processing: Use WordPress’s ?per_page=100 for large datasets, then paginate client-side.
  • WebP Conversion: Integrate with Laravel’s spatie/image-optimizer to auto-convert WordPress images to WebP.
  • Queue Delayed Syncs: Offload heavy WordPress data syncs to Laravel queues:
    dispatch(new SyncWordPressPosts($wpService))->delay(now()->addMinutes(5));
    
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