Laravel - 仅当屏幕尺寸 > 768px 时才包含文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39649811/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:30:10  来源:igfitidea点击:

Laravel - include file only if screen size > 768px

javascriptphplaravelgoogle-dfp

提问by Clinton Green

I have a tricky situation where I need to move my Google Ads around on Mobile. I figured out that the best way to do this is to call them using @include but only if certain screen size conditions are met.

我有一个棘手的情况,我需要在移动设备上移动我的 Google Ads。我发现最好的方法是使用@include 调用它们,但前提是满足某些屏幕尺寸条件。

I am thinking that the following would work

我认为以下内容会起作用

@if($screensize < 768px)
  @include('partials.my-advert')
@endif

So my question is how do I get the screen size into PHP? I can get the screen size via JS using $(window).width();but then how do I use this value in the Laravel if statement?

所以我的问题是如何将屏幕大小转换为 PHP?我可以通过 JS 获取屏幕大小,$(window).width();但是如何在 Laravel if 语句中使用这个值?

回答by Burak

You can't detect the screen size with PHP, but alternatively, you can detect the agents. There is a package, which detects the agents easily where it lets you detect whether the request is coming from desktop/tablet/mobile.

您无法使用 PHP 检测屏幕大小,但也可以检测代理。有一个,它可以轻松检测代理,让您可以检测请求是否来自桌面/平板电脑/移动设备。

To install it via composer, you can run the command below.

要通过 composer 安装它,您可以运行以下命令。

$ composer require jenssegers/agent

Then add dd the service provider to providers key within the file config/app.php.

然后将 dd 服务提供者添加到文件中的 providers 键config/app.php

Jenssegers\Agent\AgentServiceProvider::class

Furthermore, add the Agent alias to the aliases key,

此外,将代理别名添加到别名键,

'Agent' => Jenssegers\Agent\Facades\Agent::class

Finally, pass the agent variable from your controller to view.

最后,从控制器传递代理变量以查看。

$agent = new Agent();
return view('some.view', compact('agent'));

Then within your view, you can check whether the agent belongs to some mobile phone or not.

然后在您的视图中,您可以检查代理是否属于某个手机。

@if($agent->isMobile())
  @include('partials.my-advert')
@endif

回答by Sherif

There's absolutely no point in trying to do it this way, because this entire approach is antiquated. Sure, you could pass the $(window).width()from the client in javascript back to the server in PHP, but then what happens if the user resizes their window?

尝试这样做绝对没有意义,因为整个方法已经过时了。当然,您可以将$(window).width()javascript 中的客户端传递回 PHP 中的服务器,但是如果用户调整窗口大小会发生什么?

This is why, today, what is encouraged, instead, is responsive design. Even Google Adsense has responsive ad unitsfor this.

这就是为什么,今天鼓励的是响应式设计。甚至Google Adsense 也为此提供了响应式广告单元

Responsive design doesn't require the back-end to know anything about the client in order to properly render things on the page. Instead, the client makes no assumptions whatsoever about how content is rendered on the client UA, and allows advanced CSS and JS to deal with the rendering directly and responsively. Meaning, that your content is never different regardless of the screen size.

响应式设计不需要后端了解客户端的任何信息,以便在页面上正确呈现内容。相反,客户端对如何在客户端 UA 上呈现内容不做任何假设,并允许高级 CSS 和 JS 直接和响应式地处理呈现。意思是,无论屏幕大小如何,您的内容都不会有所不同。

Bootstrapis one such front-end framework that makes use of responsive design. There's also the Material design, which has many implementationsthat are also responsive.

Bootstrap就是这样一种使用响应式设计的前端框架。还有Material design,它有许多响应式的实现。