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

Is Mobile Bundle Laravel Package

backend2-plus/is-mobile-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require backend2-plus/is-mobile-bundle
    

    No additional configuration is required for basic usage.

  2. First Use Case: Inject the IsMobile service into a controller or command to check if the current request originates from a mobile device:

    use IsMobile\IsMobileBundle\IsMobile;
    
    public function index(IsMobile $isMobile): Response
    {
        $isMobileDevice = $isMobile->isMobile();
        // Use $isMobileDevice (bool) in your logic
    }
    
  3. Twig Integration (Optional): Add to config/packages/twig.yaml:

    globals:
        isMobileHelper: '@IsMobile\IsMobileBundle\IsMobile'
    

    Then use in templates:

    {% if isMobileHelper.IsMobile() %}
        <div>Mobile-specific content</div>
    {% endif %}
    

Implementation Patterns

Common Workflows

  1. Request-Based Logic: Use in controllers/middleware to serve mobile-specific responses:

    public function show(IsMobile $isMobile): Response
    {
        return $isMobile->isMobile()
            ? $this->renderMobileView()
            : $this->renderDesktopView();
    }
    
  2. Middleware for Routing: Create a middleware to redirect or modify behavior:

    public function handle(Request $request, Closure $next, IsMobile $isMobile): Response
    {
        if ($isMobile->isMobile()) {
            $request->attributes->set('mobile', true);
        }
        return $next($request);
    }
    
  3. Static Method Usage (v1.0.8+): Access via static calls where dependency injection isn’t available:

    $isMobile = IsMobile::isMobile($request);
    
  4. Twig Globals for Templates: Dynamically adjust UI based on device:

    {% if isMobileHelper.IsMobile() %}
        {{ include('mobile_nav.html.twig') }}
    {% else %}
        {{ include('desktop_nav.html.twig') }}
    {% endif %}
    

Integration Tips

  • Symfony Flex: Works seamlessly with Symfony 6.3+, 7.0+, and 8.0+.
  • Testing: Mock the IsMobile service in PHPUnit:
    $this->mock(IsMobile::class)
         ->shouldReceive('isMobile')
         ->andReturn(true);
    
  • Performance: The bundle uses lightweight HTTP user-agent checks (no external API calls).

Gotchas and Tips

Pitfalls

  1. User-Agent Spoofing:

    • Mobile detection relies on User-Agent strings, which can be spoofed. Validate results in critical paths (e.g., financial transactions).
    • Workaround: Combine with IP-based heuristics or CAPTCHA for sensitive actions.
  2. Tablet Detection:

    • The bundle may not distinguish between tablets and desktops. Test edge cases (e.g., iPad in desktop mode).
    • Tip: Extend the service to include tablet-specific logic:
      $isTablet = IsMobile::isTablet($request);
      
  3. Caching Headers:

    • Mobile detection results may change per request. Avoid caching responses that depend on isMobile() unless explicitly needed.
  4. Static Method Limitation:

    • Static methods (IsMobile::isMobile($request)) bypass Symfony’s DI container. Prefer dependency injection for testability.

Debugging

  • Log User-Agent: Add debug logs to verify detection logic:
    $userAgent = $request->headers->get('User-Agent');
    $this->logger->debug('User-Agent:', ['agent' => $userAgent]);
    
  • Test with Real Devices: Use browserstack or real devices to validate edge cases (e.g., custom browsers, bots).

Extension Points

  1. Custom Detection Logic: Extend the service by overriding the isMobile() method:

    class CustomIsMobile extends IsMobile
    {
        public function isMobile(): bool
        {
            return parent::isMobile() || $this->isCustomDevice();
        }
    }
    

    Register the custom service in services.yaml:

    IsMobile\IsMobileBundle\IsMobile: '@app.custom_is_mobile'
    
  2. Add Device-Specific Methods: Extend the bundle to include tablet/laptop detection:

    public function isTablet(): bool { /* ... */ }
    public function isLaptop(): bool { /* ... */ }
    
  3. Configuration: While the bundle has no public config options, you can bind custom logic via DI:

    # config/services.yaml
    IsMobile\IsMobileBundle\IsMobile:
        arguments:
            $customRules: !tagged 'mobile.detection.rule'
    

Tips

  • Combine with Other Packages: Use alongside symfony/mobile-detector-bundle for broader device support if needed.
  • Performance: The bundle is lightweight, but avoid calling isMobile() in loops or critical paths.
  • Document Assumptions: Clearly document mobile-specific behavior in your API/contracts (e.g., "This endpoint returns mobile-optimized data when isMobile() is true").
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle