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 Laravel Package

elao/browser-detector

Deprecated browser/user-agent detector for PHP. Provides a BrowserDetector class to load configuration and parse/set a User-Agent string. The README recommends using piwik/device-detector instead for ongoing support.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation (if still needed for legacy projects):

    composer require elao/browser-detector
    

    Note: Officially deprecated; use piwik/device-detector instead.

  2. Basic Usage (for reference only):

    use Elao\BrowserDetector\BrowserDetector;
    
    $detector = new BrowserDetector(true);
    $detector->loadConfiguration([]); // Empty array for default config
    $detector->setUserAgent($_SERVER['HTTP_USER_AGENT'] ?? '');
    
    $browser = $detector->getBrowser();
    $version = $detector->getBrowserVersion();
    
  3. First Use Case: Log browser/version in middleware or a controller:

    public function showBrowserInfo(Request $request)
    {
        $detector = new BrowserDetector(true);
        $detector->setUserAgent($request->userAgent());
        return response()->json([
            'browser' => $detector->getBrowser(),
            'version' => $detector->getBrowserVersion(),
        ]);
    }
    

Implementation Patterns

Common Workflows

  1. Middleware Integration (Legacy Approach):

    namespace App\Http\Middleware;
    
    use Elao\BrowserDetector\BrowserDetector;
    
    class DetectBrowser
    {
        public function handle($request, Closure $next)
        {
            $detector = new BrowserDetector(true);
            $detector->setUserAgent($request->userAgent());
            $request->merge(['browser' => $detector->getBrowser()]);
            return $next($request);
        }
    }
    
  2. Service Provider Binding (For Reusability):

    // app/Providers/AppServiceProvider.php
    public function register()
    {
        $this->app->singleton(BrowserDetector::class, function () {
            $detector = new BrowserDetector(true);
            $detector->loadConfiguration(config('browser_detector.config', []));
            return $detector;
        });
    }
    
  3. Configuration Management: Override default rules via config/browser_detector.php:

    return [
        'rules' => [
            'Firefox' => [
                'pattern' => '/Firefox\/(\d+\.\d+)/',
                'name'    => 'Firefox',
            ],
        ],
    ];
    
  4. Caching Results (Performance Optimization):

    $cacheKey = 'browser_'.$request->ip().'_'.$request->userAgent();
    $browser = cache()->remember($cacheKey, 3600, function () use ($detector) {
        return $detector->getBrowser();
    });
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warning:

    • The package is archived and deprecated. Use piwik/device-detector for new projects.
    • Migration tip: Replace getBrowser() with getClient()->getBrowserName() in the new package.
  2. Outdated Patterns:

    • Assumes $_SERVER['HTTP_USER_AGENT'] directly. Modern Laravel uses $request->userAgent().
    • No support for mobile/OS detection (unlike piwik/device-detector).
  3. Configuration Quirks:

    • loadConfiguration([]) expects an array of rules, not a file path.
    • Default rules are hardcoded; custom rules must be merged manually.
  4. Performance:

    • Regex-based detection can be slow for high-traffic apps. Cache aggressively.

Debugging Tips

  1. Verify User Agent:

    dd($_SERVER['HTTP_USER_AGENT'] ?? $request->userAgent());
    
    • Test with known strings (e.g., "Mozilla/5.0 (Windows NT 10.0; Win64; x64)").
  2. Inspect Rules:

    $detector->getRules(); // Dump loaded patterns
    
  3. Fallback Logic:

    $browser = $detector->getBrowser() ?: 'Unknown';
    

Extension Points

  1. Custom Rules: Extend via loadConfiguration():

    $detector->loadConfiguration([
        'CustomBrowser' => [
            'pattern' => '/CustomApp\/(\d+\.\d+)/',
            'name'    => 'CustomBrowser',
        ],
    ]);
    
  2. Event Hooks (Legacy): Override BrowserDetector class to add pre/post-processing:

    class ExtendedDetector extends BrowserDetector
    {
        public function getBrowser()
        {
            $browser = parent::getBrowser();
            // Add custom logic (e.g., log unknown browsers)
            return $browser;
        }
    }
    
  3. Integration with Laravel Scout: For analytics, pair with Scout behaviors:

    Scout::behavior(function ($model, $driver) {
        $browser = app(BrowserDetector::class)->getBrowser();
        return ['browser' => $browser];
    });
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky