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

Mobile Detect Laravel Package

diego182/mobile-detect

Symfony bundle wrapper for Mobile Detect. Provides auto-configuration and autowiring so you can inject a MobileDetect service into your app to detect mobile/tablet devices from the user agent with minimal setup.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require diego182/mobile-detect
    

    Register the service provider in config/app.php:

    'providers' => [
        // ...
        Diego182\MobileDetectBundle\MobileDetectServiceProvider::class,
    ],
    
  2. Basic Usage Inject the MobileDetect service into a controller or service:

    use Diego182\MobileDetectBundle\MobileDetect;
    
    public function __construct(private MobileDetect $detect) {}
    
    public function checkDevice(Request $request) {
        if ($this->detect->isMobile()) {
            return response()->json(['device' => 'mobile']);
        }
        return response()->json(['device' => 'desktop']);
    }
    
  3. First Use Case Redirect mobile users to a mobile-optimized URL:

    if ($this->detect->isMobile()) {
        return redirect()->to('/mobile');
    }
    

Implementation Patterns

Common Workflows

  1. Device-Specific Logic Use isMobile(), isTablet(), or isBot() to conditionally render views or APIs:

    if ($this->detect->isTablet()) {
        return view('tablet.view');
    }
    
  2. User-Agent Parsing Extract device details (e.g., OS, browser) for analytics:

    $os = $this->detect->getOsName();
    $browser = $this->detect->getBrowserName();
    
  3. Middleware Integration Create middleware to enforce device restrictions:

    namespace App\Http\Middleware;
    
    use Closure;
    use Diego182\MobileDetectBundle\MobileDetect;
    
    class MobileOnly
    {
        public function __construct(private MobileDetect $detect) {}
    
        public function handle($request, Closure $next) {
            if (!$this->detect->isMobile()) {
                abort(403, 'Mobile devices only.');
            }
            return $next($request);
        }
    }
    
  4. API Versioning Route mobile traffic to a lightweight API endpoint:

    Route::middleware(['mobile.detect'])->group(function () {
        Route::get('/api/v1/mobile', 'MobileController@index');
    });
    

Gotchas and Tips

Pitfalls

  1. Caching Headers User-agent strings can change frequently. Avoid caching responses based solely on device detection unless explicitly needed.

  2. False Positives Some bots/tablets may misclassify. Test edge cases:

    if ($this->detect->isMobile() && !$this->detect->isTablet()) {
        // Only mobile phones
    }
    
  3. Performance Overhead Parsing user-agent strings adds minimal overhead, but avoid calling MobileDetect in critical loops.

Debugging Tips

  • Log User-Agent Strings For debugging, log the raw user-agent:
    \Log::debug('User-Agent:', [$request->userAgent()]);
    
  • Update Regularly The package relies on underlying libraries (e.g., mobile-detect). Monitor for updates to the base package.

Extension Points

  1. Custom Device Rules Extend the detector by subclassing MobileDetect:

    class CustomMobileDetect extends \Mobile_Detect {
        public function isCustomDevice() {
            return $this->is('CustomDeviceName');
        }
    }
    

    Register the custom class in the service provider.

  2. Integration with Laravel Scout Use device detection for search personalization:

    if ($this->detect->isMobile()) {
        $results = Product::search($query)->mobileResults()->get();
    }
    
  3. Configuration Override default settings via config/mobile-detect.php:

    'tablet_user_agents' => [
        'iPad', // Add custom tablet agents
    ],
    
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