Laravel检测手机/平板电脑并加载正确的视图 [英] Laravel detect mobile/tablet and load correct views
问题描述
我已经阅读了如何为视图添加不同的路径或名称空间,但是我认为这对我来说不是一个合适的解决方案. 我想做的是为移动设备设置一个视图基本路径,为桌面设备设置一个不同的视图路径,因此在视图控制器中,我不需要进行任何更改.
在路由文件中设置路径,并且不要触摸任何视图控制器,那将是很好的选择.有什么想法吗?也许只是Config :: set视图路径?
提前谢谢! :)
我在这里也遇到了同样的问题,基本上是希望在不影响我的控制器的情况下固定"移动视图目录(如果可能的话). /p>
执行此操作的一个地方可能是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
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! :)
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
这篇关于Laravel检测手机/平板电脑并加载正确的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!