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

Laravel Device Detector Laravel Package

sajidwarner/laravel-device-detector

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require sajidwarner/laravel-device-detector
    

    Auto-discovery handles service provider/facade registration.

  2. Publish Config (if customization needed):

    php artisan vendor:publish --provider="SajidWarner\LaraTrack\LaraTrackServiceProvider"
    
  3. First Use Case: Detect device type in a controller:

    use SajidWarner\LaraTrack\Facades\LaraTrack;
    
    public function show(Request $request) {
        $device = LaraTrack::detect($request);
        if ($device['is_mobile']) {
            return view('mobile.view');
        }
        return view('desktop.view');
    }
    

Where to Look First

  • Facade Methods: LaraTrack::detect(), LaraTrack::isMobile(), LaraTrack::getBrowser()
  • Blade Directives: @mobile, @desktop, @robot (for quick UI conditionals)
  • Middleware: Predefined aliases like laratrack.block-bots (no manual registration)

Implementation Patterns

Core Workflows

  1. Request-Based Detection:

    $device = LaraTrack::detect($request); // Pass request object
    
    • Why? Ensures accurate header/UA parsing (e.g., Sec-CH-UA for modern browsers).
  2. Middleware Integration:

    Route::middleware(['laratrack.block-bots', 'laratrack.mobile-only'])->group(...);
    
    • Pattern: Group routes by device/access type (e.g., block bots from sensitive endpoints).
  3. Event-Driven Actions:

    // EventServiceProvider.php
    protected $listen = [
        BotDetected::class => [LogBotAction::class],
    ];
    
    • Use Case: Log or block suspicious traffic (e.g., Tor/VPN) without middleware clutter.
  4. Blade Templating:

    @mobile
        <link rel="stylesheet" href="/mobile.css">
    @endmobile
    
    • Pattern: Separate mobile/desktop styles/scripts without PHP logic.

Integration Tips

  • Geolocation: Enable via .env (LARATRACK_GEO_ENABLED=true) and cache responses per-IP.
  • Custom Logic: Extend detection with LaraTrack::extend() (e.g., add custom bot signatures).
  • Testing: Use php artisan laratrack:test with custom UAs/IPs to validate edge cases.

Gotchas and Tips

Pitfalls

  1. Geolocation API Limits:

    • Free tier (30k/month) may throttle high-traffic sites.
    • Fix: Cache responses aggressively (LARATRACK_GEO_CACHE_MINUTES=1440).
  2. Tor/VPN Detection False Positives:

    • Some corporate networks use VPNs legitimately.
    • Fix: Whitelist IPs in config/laratrack.php:
      'allowed_ips' => ['192.168.1.0/24'],
      
  3. Blade Directives in Loops:

    • @mobile directives render once per view. For dynamic content, use PHP checks:
      @if(LaraTrack::isMobile()) ... @endif
      
  4. Client Hints (Sec-CH-UA):

    • Modern browsers (Chrome 114+) prefer Sec-CH-UA over User-Agent.
    • Tip: Test with php artisan laratrack:test using:
      Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
      Sec-CH-UA: "Not_A Brand";v="99", "Google Chrome";v="120", "Chromium";v="120"
      

Debugging

  • Artisan Command: Test detection interactively:
    php artisan laratrack:test "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0)"
    
  • Log Raw Data: Dump full detection array:
    \Log::debug('Device Data', LaraTrack::detect($request)->toArray());
    

Extension Points

  1. Custom Bot Signatures:

    LaraTrack::extend('bot', function ($userAgent) {
        return str_contains($userAgent, 'CustomBot');
    });
    
  2. Override Middleware Messages:

    // config/laratrack.php
    'middleware' => [
        'bot_message' => 'Access denied: {botName}',
    ];
    
  3. Disable Features:

    // config/laratrack.php
    'disable_geolocation' => env('APP_ENV') === 'local',
    'fire_events' => false,
    

Performance

  • Caching: Geolocation and Tor checks cache per-IP by default (TTL: 1 hour).
  • Disable Unused Features: Set LARATRACK_GEO_ENABLED=false if geolocation isn’t needed.
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.
monarobase/country-list
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity