Laravel 检测手机/平板电脑并加载正确的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23779088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel detect mobile/tablet and load correct views
提问by Ayoze Roberto Bernardo
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?
在路由文件中设置路径并且不接触任何视图控制器会很棒。有什么想法吗?也许只是 Config::set 视图路径?
Thanks in advance! :)
提前致谢!:)
采纳答案by Adam Marshall
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
:
执行此操作的一个地方可能是以下配置中的配置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
PS:代理 ~= Mobile_Detect
回答by timgavin
For you Laravel 5 users in the future who are looking for a way to detect devices in the view; another option is to create a ServiceProvier - and then use View::share()
- which will then make the device-detecting $agent
available inside all of your views.
对于未来正在寻找一种在视图中检测设备的方法的 Laravel 5 用户;另一种选择是创建一个 ServiceProvier - 然后使用View::share()
- 这将使设备检测$agent
在您的所有视图中可用。
Install Agent
安装代理
composer require jenssegers/agent
Create the service provider
创建服务提供者
php artisan make:provider AgentServiceProvider
In config/app.php
在 config/app.php
App\Providers\AgentServiceProvider::class,
in app/providers/AgentServiceProvider.php
在 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()
{
//
}
}
Then inside your views
然后在你的意见里面
@if ($agent->isMobile())
Show mobile stuff...
@endif
回答by Sven van Zoelen
You could create two folders mobile
, desktop
inside your view folder. The two folders hold the same views (only the filenames).
您可以创建两个文件夹mobile
,desktop
您的视图文件夹内。这两个文件夹拥有相同的视图(只有文件名)。
├── views
| ├── mobile
| | ├── main.blade.php
| └── desktop
| ├── main.blade.php
Then inside your controller you can use the folder names to switch between the desktop and mobile views (or any other if you add more).
然后在您的控制器中,您可以使用文件夹名称在桌面和移动视图之间切换(如果添加更多,则可以使用任何其他视图)。
You only need to resolve the request's device through PHP. You can do it with this project: http://mobiledetect.net/.
您只需要通过 PHP 解析请求的设备。你可以用这个项目做到这一点:http: //mobiledetect.net/。
Now your controller looks like:
现在你的控制器看起来像:
public function getIndex() {
$detect = new Mobile_Detect;
return View::make( ($detect->isMobile() ? 'mobile' : 'desktop') . '.your-view-name' );
}
It's offcourse a good idea to refactor the ($detect->isMobile() ? 'mobile' : 'desktop')
to a helper/static function. Or register it as an config item in a before route filter.
将 重构($detect->isMobile() ? 'mobile' : 'desktop')
为辅助函数/静态函数当然是个好主意。或者在路由过滤器之前将其注册为配置项。
回答by kiki
As suggested in a comment on the accepted answer (include mobile view path only on mobile and fallback to '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 override what you need. This may come in handy for emails and when you have several partial views that have the same html regardless of device category.
这样,您只需覆盖您需要的内容。这对于电子邮件以及当您有多个具有相同 html 的部分视图时可能会派上用场,而不管设备类别如何。
Note: Usage in controller doesn't change.
注意:控制器中的用法不会改变。
Example views:
示例视图:
├── 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