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

Browser Detector Bundle Laravel Package

elao/browser-detector-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require elao/browser-detector-bundle:1.1.*
    

    Add to AppKernel.php:

    new Elao\Bundle\BrowserDetectorBundle\ElaoBrowserDetectorBundle(),
    
  2. Basic Configuration (config.yml):

    elao_browser_detector:
        browsers:
            incompatible:
                "IE": "<=7"
    
  3. First Use Case: Inject the service in a controller to check browser compatibility:

    use Elao\Bundle\BrowserDetectorBundle\Service\BrowserDetector;
    
    class HomeController extends Controller
    {
        public function index(BrowserDetector $detector)
        {
            if ($detector->isCompatible()) {
                return $this->render('homepage');
            }
            return $this->redirectToFallback();
        }
    }
    

Implementation Patterns

Common Workflows

  1. Detecting Browser Compatibility:

    if (!$detector->isCompatible()) {
        $this->addFlash('error', 'Your browser is not supported.');
        return $this->redirectToRoute('browser_fallback');
    }
    
  2. Partial Compatibility Handling:

    elao_browser_detector:
        browsers:
            partially_compatible:
                "Internet Explorer": "<9"
    

    Use isPartiallyCompatible() to trigger feature flags or warnings.

  3. User-Agent Parsing: Access raw data via getBrowser() or getVersion():

    $browserName = $detector->getBrowser()->getName();
    $browserVersion = $detector->getBrowser()->getVersion();
    
  4. Event-Based Integration: Listen to kernel.request for real-time checks:

    $eventDispatcher->addListener('kernel.request', function (GetResponseEvent $event) {
        $detector = $event->getRequest()->get('elao_browser_detector');
        if (!$detector->isCompatible()) {
            $event->setResponse(new RedirectResponse('/fallback'));
        }
    });
    
  5. Custom Logic with BrowserCap: Extend functionality by injecting ElaoBrowserDetector service:

    $browserCap = $detector->getBrowserCap();
    if ($browserCap->isMobile()) {
        // Mobile-specific logic
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warning:

  2. Version Format Strictness:

    • Invalid version formats (e.g., "Firefox": "3.6-4.0") will silently fail. Validate inputs:
      if (!$detector->isVersionValid('Firefox', '>3.6')) {
          throw new \InvalidArgumentException('Invalid version format.');
      }
      
  3. Case Sensitivity:

    • Browser names in config (e.g., "IE") must match exactly the parsed name. Use getBrowser()->getName() to debug mismatches.
  4. Caching Issues:

    • The bundle does not cache user-agent parsing by default. For high-traffic apps, cache the BrowserDetector service:
      services:
          elao_browser_detector:
              public: true
              synthetic: true
      
  5. Kernel Event Dependency:

    • The bundle only runs on kernel.request. For CLI commands or non-request contexts, manually instantiate:
      $detector = new \Elao\BrowserDetector\BrowserDetector($requestStack->getCurrentRequest());
      

Debugging Tips

  1. Log Raw User-Agent:

    $userAgent = $request->headers->get('User-Agent');
    $this->logger->debug('User-Agent:', ['ua' => $userAgent]);
    
  2. Verify Configuration: Dump the parsed browsers:

    dump($detector->getBrowser()->getName(), $detector->getVersion());
    
  3. Test Edge Cases:

Extension Points

  1. Custom Browser Rules: Extend the BrowserCap class to add proprietary checks:

    class CustomBrowserCap extends \Elao\BrowserDetector\BrowserCap
    {
        public function isEnterpriseBrowser()
        {
            return strpos($this->getName(), 'Enterprise') !== false;
        }
    }
    
  2. Override Default Logic: Replace the service definition in services.yml:

    services:
        elao_browser_detector:
            class: AppBundle\Service\CustomBrowserDetector
            arguments: ['@request_stack']
    
  3. Add Metadata: Attach browser data to the request for downstream use:

    $request->attributes->set('browser', [
        'name' => $detector->getBrowser()->getName(),
        'version' => $detector->getVersion(),
        'is_compatible' => $detector->isCompatible(),
    ]);
    
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