Laravel 检测手机/平板电脑并加载正确的视图 [英] Laravel detect mobile/tablet and load correct views

查看:19
本文介绍了Laravel 检测手机/平板电脑并加载正确的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

提前致谢!:)

推荐答案

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

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

这样做的一个地方可能是 app/config/views.php 中的配置:

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

<?php

use JenssegersAgentAgent 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:代理 ~= Mobile_Detect

PS: Agent ~= Mobile_Detect

这篇关于Laravel 检测手机/平板电脑并加载正确的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆