【问题标题】:Laravel detect mobile/tablet and load correct viewsLaravel 检测手机/平板电脑并加载正确的视图
【发布时间】:2014-07-09 20:29:17
【问题描述】:

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

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

提前致谢! :)

【问题讨论】:

  • 这是个坏主意。嗅探用户代理总是有问题的。更好的是,创建两个子域,wwwmobile,让用户选择他们想要的任何东西。根据子域的不同,您可以创建一个环境,稍后只需恢复 env,您就可以调整任何您需要的东西。
  • 我不确定.. 营销信息: “请访问我们的网站 www.thing.com,除非您使用的是手机,在这种情况下请转至 mobile.thing.com!”我觉得不对劲..

标签: laravel laravel-4


【解决方案1】:

您可以在视图文件夹中创建两个文件夹mobiledesktop。这两个文件夹拥有相同的视图(只有文件名)。

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

然后在您的控制器中,您可以使用文件夹名称在桌面视图和移动视图之间切换(或者添加更多视图)。

您只需要通过 PHP 解析请求的设备即可。你可以用这个项目做到这一点:http://mobiledetect.net/

现在你的控制器看起来像:

public function getIndex() {
    $detect = new Mobile_Detect;

    return View::make( ($detect->isMobile() ? 'mobile' : 'desktop') . '.your-view-name' );
}

($detect->isMobile() ? 'mobile' : 'desktop') 重构为辅助/静态函数当然是个好主意。或者在路由过滤器之前将其注册为配置项。

【讨论】:

  • 首先,谢谢!但重点是不要更改我的代码中的所有 View::make。
  • 有时需要重构。您还可以使用类扩展View,然后在构造函数中处理移动/桌面切换。那么你只需要更改代码中的所有uses即可。
【解决方案2】:

我在这里看到了同样的问题,基本上是想“固定”移动视图的目录而不弄乱我的控制器(如果可能的话)。

这样做的一个地方可能是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),
    .....

似乎有效,为您提供了一个完全不同的目录。

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

PS:代理 ~= Mobile_Detect

【讨论】:

  • 如果您始终离开视图路径并且只在移动设备上包含移动设备,您可以在不需要更改时安全地退回到正常视图
【解决方案3】:

正如对已接受答案的评论中所建议的那样(仅在移动设备上包括移动视图路径并回退到“默认”视图):

<?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
    ...

这样你只覆盖你需要的东西。这对于电子邮件以及当您有多个具有相同 html 的部分视图(无论设备类别如何)时可能会派上用场。

注意:控制器中的用法不会改变。

示例视图:

├── 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

【讨论】:

    【解决方案4】:

    为你未来的 Laravel 5 用户寻找一种方法来检测视图中的设备;另一种选择是创建一个 ServiceProvier - 然后使用 View::share() - 这将使设备检测 $agent 在您的所有视图中可用。

    安装Agent

    composer require jenssegers/agent
    

    创建服务提供者

    php artisan make:provider AgentServiceProvider
    

    在 config/app.php 中

    App\Providers\AgentServiceProvider::class,
    

    在 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()
        {
            //
        }
    }
    

    然后在你的意见中

    @if ($agent->isMobile())
    
        Show mobile stuff...
    
    @endif
    

    【讨论】:

    • Agent 是一个综合包,因为它是一个检测器,可以检测移动设备、浏览器、名称版本、平台、设备、机器人、爬虫、用户语言。如果你想学习如何使用你也可以阅读这个medium.com/@panjeh/…
    • 代理是否仍在使用/最佳选择?我注意到它的 Git 构建失败了,但我不知道有什么更好的选择?
    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多