Weave 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

Predictionio Bundle Laravel Package

codag/predictionio-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require codag/predictionio-bundle:dev-master
    

    Add to AppKernel.php:

    new Codag\PredictionIOBundle\CodagPredictionIOBundle(),
    
  2. Configuration (config.yml):

    codag_prediction_io:
        app_key: "your_predictionio_app_key"
        api_url: "http://your-predictionio-server:8000"
    
  3. First Use Case: Inject the service and call a PredictionIO method (e.g., trackEvent):

    use Codag\PredictionIOBundle\Service\PredictionIOService;
    
    class MyController extends Controller
    {
        public function index(PredictionIOService $predictionIO)
        {
            $predictionIO->trackEvent(
                'user123', // user ID
                'product_view', // event name
                ['productId' => 'prod456'] // event data
            );
            return new Response('Event tracked!');
        }
    }
    

Where to Look First

  • Service Class: Codag\PredictionIOBundle\Service\PredictionIOService (wraps the PredictionIO-PHP-SDK).
  • Configuration: app/config/config.yml for app_key and api_url.
  • SDK Docs: PredictionIO-PHP-SDK for method signatures.

Implementation Patterns

Core Workflows

  1. Event Tracking: Log user actions (e.g., clicks, purchases) for real-time recommendations:

    $predictionIO->trackEvent('user789', 'purchase', ['itemId' => '123']);
    
  2. Recommendations: Fetch real-time or batch recommendations:

    $recommendations = $predictionIO->recommendItems('user456', 'product', 5);
    
  3. User Segmentation: Query user segments (e.g., "high-value customers"):

    $users = $predictionIO->getUsersInSegment('high_value');
    

Integration Tips

  • Dependency Injection: Use the PredictionIOService in controllers/services via autowiring or manual injection.
  • Event Listeners: Trigger PredictionIO events in Symfony event listeners (e.g., KernelEvents::TERMINATE):
    public function onKernelTerminate(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        $predictionIO->trackEvent($request->get('userId'), 'page_view', ['url' => $request->getUri()]);
    }
    
  • Batch Processing: Use async tasks (e.g., Symfony Messenger) for bulk event tracking to avoid API throttling.

Common Use Cases

Use Case Example Code
Track e-commerce events $predictionIO->trackEvent('user1', 'add_to_cart', ['productId' => '1001']);
Get personalized recs $recs = $predictionIO->recommendItems('user1', 'movie', 3);
Update user profile $predictionIO->setUserProfile('user1', ['age' => 30, 'gender' => 'M']);

Gotchas and Tips

Pitfalls

  1. Deprecated SDK:

    • The underlying PredictionIO-PHP-SDK is outdated (last updated in 2015). Methods may not align with newer PredictionIO versions (e.g., v0.12+).
    • Workaround: Check the PredictionIO REST API docs and manually adapt calls if needed.
  2. Configuration Sensitivity:

    • Hardcoding app_key or api_url in config.yml exposes secrets. Use Symfony’s %env% or parameter bags:
      codag_prediction_io:
          app_key: "%env(PREDICTIONIO_APP_KEY)%"
      
  3. Error Handling:

    • The bundle lacks built-in exception handling for PredictionIO API errors (e.g., rate limits, invalid requests).
    • Tip: Wrap calls in try-catch and log errors:
      try {
          $predictionIO->trackEvent(...);
      } catch (\Exception $e) {
          $this->container->get('logger')->error('PredictionIO error: ' . $e->getMessage());
      }
      
  4. CORS/Proxy Issues:

    • If PredictionIO runs on a separate domain, configure CORS or use a Symfony proxy (e.g., HttpClient with a custom adapter).

Debugging Tips

  • Enable Debug Mode: Set debug: true in the bundle config to log raw API responses:
    codag_prediction_io:
        debug: true
    
  • Check SDK Logs: The underlying SDK logs HTTP requests/responses to PredictionIO.log (if configured).

Extension Points

  1. Custom Services: Extend PredictionIOService to add domain-specific methods:

    class CustomPredictionIOService extends PredictionIOService
    {
        public function trackCartUpdate(string $userId, array $cartItems): void
        {
            $this->trackEvent($userId, 'cart_update', ['items' => $cartItems]);
        }
    }
    
  2. Event Subscribers: Create subscribers to auto-track events (e.g., Doctrine lifecycle events):

    class CartSubscriber implements EventSubscriber
    {
        public function getSubscribedEvents()
        {
            return [
                'kernel.request' => 'onKernelRequest',
            ];
        }
    
        public function onKernelRequest(GetResponseEvent $event)
        {
            $cart = $event->getRequest()->get('cart');
            $predictionIO->trackEvent('user1', 'cart_update', $cart);
        }
    }
    
  3. Async Processing: Use Symfony’s Messenger component to queue PredictionIO calls for background processing:

    $message = new TrackPredictionIOEvent('user1', 'event_name', ['data']);
    $this->messageBus->dispatch($message);
    

Configuration Quirks

  • API URL Format: Ensure api_url ends with / (e.g., http://example.com/api/). The SDK may prepend paths incorrectly.
  • SSL/Warnings: If using self-signed certificates, disable SSL verification in the SDK config (not recommended for production):
    codag_prediction_io:
        ssl_verification: false  # Avoid in prod!
    
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.
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels
wazza/laravel-db-encryption
hokny/laravel-generator
vonsogt/backup
jengo/queues
jeremykenedy/laravel-observability
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums