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

Browscap Laravel Package

crossjoin/browscap

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require crossjoin/browscap
    

    For PHP 7.x (latest), 5.6 (v2.x), or 5.3 (v1.x), ensure compatibility aligns with your project.

  2. Basic Usage

    use Crossjoin\Browscap\Browscap;
    
    $browscap = new Browscap();
    $browser  = $browscap->getBrowser('Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1');
    
    // Output: Array with device/browser properties (e.g., `platform`, `device_type`, `browser_name`).
    print_r($browser);
    
  3. First Use Case

    • User-Agent Parsing: Detect device/OS/browser in a middleware or controller:
      $ua = request()->header('User-Agent');
      $browser = $browscap->getBrowser($ua);
      return response()->json(['device' => $browser['device_type']]);
      

Implementation Patterns

Core Workflows

  1. Middleware Integration Parse user agents globally for analytics or conditional logic:

    namespace App\Http\Middleware;
    
    use Crossjoin\Browscap\Browscap;
    
    class DetectBrowser
    {
        protected $browscap;
    
        public function __construct(Browscap $browscap)
        {
            $this->browscap = $browscap;
        }
    
        public function handle($request, Closure $next)
        {
            $request->browser = $this->browscap->getBrowser($request->header('User-Agent'));
            return $next($request);
        }
    }
    
  2. Caching Strategies Cache results for performance (e.g., Redis):

    $cacheKey = 'browser_' . md5($ua);
    $browser  = cache()->remember($cacheKey, now()->addHours(1), function() use ($browscap, $ua) {
        return $browscap->getBrowser($ua);
    });
    
  3. Automatic Updates Enable auto-updates via config (config/browscap.php):

    'update' => [
        'enabled' => true,
        'frequency' => 'daily', // or 'weekly'
    ],
    

Advanced Patterns

  • Custom Fields: Extend the parser with additional logic:

    $browser = $browscap->getBrowser($ua);
    $browser['is_mobile'] = in_array($browser['device_type'], ['smartphone', 'tablet']);
    
  • Bulk Processing:

    $uas = ['UA1', 'UA2', 'UA3'];
    $results = array_map([$browscap, 'getBrowser'], $uas);
    

Gotchas and Tips

Pitfalls

  1. User-Agent Spoofing

    • Malicious or spoofed UAs may return inaccurate results. Validate with fallback logic:
      if (empty($browser['browser_name'])) {
          $browser = ['browser_name' => 'Unknown', 'platform' => 'Unknown'];
      }
      
  2. Memory Usage

    • Large-scale parsing (e.g., logs) may spike memory. Use getBrowser() sparingly or batch-process.
  3. Update Conflicts

    • Auto-updates may fail if storage/browscap lacks write permissions. Set:
      chmod -R 755 storage/browscap
      

Debugging

  • Verify Data:
    $browscap->getVersion(); // Check installed Browscap version.
    $browscap->getLastUpdate(); // Confirm auto-update status.
    
  • Log Unknown UAs:
    if (empty($browser['browser_name'])) {
        \Log::warning("Unknown UA: {$ua}");
    }
    

Extension Points

  1. Custom Data Files Override the default Browscap file path in config:

    'path' => storage_path('custom/browscap.ini'),
    
  2. Hooks for Post-Processing Extend the Browscap class to add custom logic:

    class CustomBrowscap extends Browscap
    {
        public function getBrowser($ua)
        {
            $browser = parent::getBrowser($ua);
            $browser['custom_field'] = $this->customLogic($browser);
            return $browser;
        }
    }
    
  3. Performance Tuning Disable unused features (e.g., parse_ua_string if only checking cached results).

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.
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope