Laravel检测移动/平板电脑并加载正确的视图

Ayo*_*rdo 11 laravel laravel-4

我已经阅读了如何为视图添加不同的路径或命名空间,但我认为这对我来说不是一个合适的解决方案.我想要做的是为移动设备设置视图基本路径,为桌面设备设置不同的视图基本路径,因此在视图控制器中我不需要进行任何更改.

在路径文件中设置路径并且不要触摸任何视图控制器会很棒.有什么想法吗?也许只是Config ::设置视图路径?

提前致谢!:)

tim*_*vin 28

对于未来正在寻找方法来检测视图中设备的Laravel 5用户; 另一个选择是创建一个ServiceProvier - 然后使用View::share()- 然后$agent在所有视图中使设备检测可用.

安装代理

composer require jenssegers/agent
Run Code Online (Sandbox Code Playgroud)

创建服务提供商

php artisan make:provider AgentServiceProvider
Run Code Online (Sandbox Code Playgroud)

在config/app.php中

App\Providers\AgentServiceProvider::class,
Run Code Online (Sandbox Code Playgroud)

在app/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()
    {
        //
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的意见内

@if ($agent->isMobile())

    Show mobile stuff...

@endif
Run Code Online (Sandbox Code Playgroud)


Ada*_*all 11

我在这里看同样的问题,基本上想要"插上"移动视图的目录而不会弄乱我的控制器(如果可能的话).

一个地方可能是配置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),
    .....
Run Code Online (Sandbox Code Playgroud)

似乎工作,给你一个完全不同的目录.

我会继续尝试,因为桌面和移动设备之间可能会有一些重叠,但我们会看到.

PS:Agent〜= Mobile_Detect