Laravel detects mobile / tablet and loads correct views

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

It would be great to set the path in the routes file and not touch any view controller. Any ideas for this? Maybe just Config :: set the view path?

Thanks in advance!:)

+7
laravel laravel-4
source share
4 answers

I look at the same problem here, mostly wanting to “lock up” the directory of mobile views without getting confused with my controllers (if possible).

One place for this could be 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 with.

I will continue to experiment, as there may be some overlap between the desktop and mobile applications, but we will see.

PS: Agent ~ = Mobile_Detect

+7
source share

For you, Laravel 5 future users who are looking for a way to discover devices in the view; another option is to create a ServiceProvier - and then use View::share() - which will then make $agent device discovery available in all of your views.

Install Agent

 composer require jenssegers/agent 

Create a service provider

 php artisan make:provider AgentServiceProvider 

In config / app.php

 App\Providers\AgentServiceProvider::class, 

in application / providers / AgentServiceProvider.php

 <?php namespace App\Providers; use View; use Jenssegers\Agent\Agent; use Illuminate\Support\ServiceProvider; class AgentServiceProvider extends ServiceProvider { public function boot() { $agent = new Agent(); View::share('agent', $agent); } public function register() { // } } 

Then inside your views

 @if ($agent->isMobile()) Show mobile stuff... @endif 
+12
source share

You can create two mobile , desktop folders inside your view folder. Two folders contain the same views (file names only).

 ├── views | ├── mobile | | ├── main.blade.php | └── desktop | ├── main.blade.php 

Then, inside your controller, you can use the folder names to switch between viewing the desktop and mobile device (or any other if you add more).

You only need to allow the request device through PHP. You can do this with this project: http://mobiledetect.net/ .

Your controller now looks like this:

 public function getIndex() { $detect = new Mobile_Detect; return View::make( ($detect->isMobile() ? 'mobile' : 'desktop') . '.your-view-name' ); } 

It is a good idea to refactor ($detect->isMobile() ? 'mobile' : 'desktop') to a helper / static function. Or register it as a configuration item in the filters before the route.

+2
source share

As suggested in the commentary on the accepted answer (including the mobile browsing path only on the mobile device and to return to the default view):

 <?php $viewBasePath = realpath(base_path('resources/views')); $viewsPaths = [$viewBasePath]; $agent = new Jenssegers\Agent\Agent(); if ($agent->isMobile()) { array_unshift($viewsPaths, $viewBasePath.'/mobile'); } return [ 'paths' => $viewsPaths ... 

This way you only redefine what you need. This can come in handy for emails, and when you have several partial views that have the same html regardless of device category.

Note. Use in the controller does not change.

Example:

 ├── views | ├── home.blade.php | ├── posts.blade.php | ├── post.blade.php | ├── emails | | └── subscription.blade.php | └── partials | | ├── posts-popular.blade.php | | ├── banner-ad.blade.php | | ├── post-comment.blade.php | ├── mobile | | ├── home.blade.php | | ├── partials | | └── posts-popular.blade.php 
+1
source share

All Articles