laravellaravel-4

Laravel detect mobile/tablet and load correct views


I have read how to add different paths or namespaces for views, but I think this is not a proper solution for me. What I would like to do it's to set a view base path for mobile devices and a different one for desktop devices, so in the view controllers I don't need to make any change.

That would be great to set the path in the routes file and don't touch any view controller. Any ideas for that? Maybe just Config::set the view path?

Thanks in advance! :)


Solution

  • I'm looking at the same issue here, basically wanting to "bolt on" a directory of mobile views without messing with my controllers (if possible).

    One place to do this may be the config in app/config/views.php:

    <?php
    
    use Jenssegers\Agent\Agent as Agent;
    $Agent = new Agent();
    // agent detection influences the view storage path
    if ($Agent->isMobile()) {
        // you're a mobile device
        $viewPath = __DIR__.'/../mobile';
    } else {
        // you're a desktop device, or something similar
        $viewPath = __DIR__.'/../views';
    }
    
    
    return array(
        'paths' => array($viewPath),
        .....
    

    seems to work, giving you a completely different directory to work from.

    I'll continue to experiment, as perhaps there will be some overlap between the desktop and mobile includes, but we'll see.

    PS: Agent ~= Mobile_Detect