zenstruck / mobile-bundle
管理您的 Symfony2 应用程序的移动版本的工具
Requires
- symfony/framework-bundle: ~2.1
This package is not auto-updated.
Last update: 2022-02-01 12:20:10 UTC
README
为 Symfony2 的移动开发添加各种工具。
安装
-
在您的
deps
文件中添加以下行[ZenstruckMobileBundle] git=git://github.com/kbond/ZenstruckMobileBundle.git target=bundles/Zenstruck/Bundle/MobileBundle
-
将
Zenstruck
命名空间添加到您的自动加载器<?php // app/autoload.php $loader->registerNamespaces(array( // ... 'Zenstruck' => __DIR__.'/../vendor/bundles', ));
-
最后,在内核中启用此包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Zenstruck\Bundle\MobileBundle\ZenstruckMobileBundle(), ); }
移动管理器
zenstruck_mobile.manager
服务存储当前请求是否为移动请求。
设置
在您的 config.yml
zenstruck_mobile:
full_host: example.com
mobile_host: m.example.com
您还可以设置当前环境的移动状态(即 config_mobile.yml
)
zenstruck_mobile:
mobile: true/false
这允许您将虚拟主机指向 index.php
以启用 full
环境,并将另一个虚拟主机指向 mobile.php
以启用 mobile
环境。
示例
<?php
$this->container->get('zenstruck_mobile.manager')->isMobile(); // true or false
$this->container->get('zenstruck_mobile.manager')->getMobile(); // false or 'mobile'
$this->container->get('zenstruck_mobile.manager')->getMobileHost(); // your mobile_host set in config.yml
$this->container->get('zenstruck_mobile.manager')->getFullHost(); // your full_host set in config.yml
监听器
为了使您的完整网站和移动网站处于同一环境中,此包提供了一个事件监听器。该监听器将当前请求的主机名与您在 config.yml
中设置的 mobile_host
进行比较。如果找到匹配项,则将管理器的移动标志设置为 true。
在您的 config.yml
中启用/禁用监听器
zenstruck_mobile:
use_listener: true/false
Twig 辅助函数
有一些twig函数允许您生成完整/移动URL。
在您的 config.yml
中启用/禁用它们
zenstruck_mobile:
use_helper: true/false
在您的twig模板中使用它们,如下所示
{{ zenstruck_mobile_url() }} {# generates mobile url #}
{{ zenstruck_mobile_full_url() }} {# generates full url #}
{{ zenstruck_mobile_is_mobile() }} {# true if mobile #}
可选参数
zenstruck_mobile_url()
和zenstruck_mobile_full_url()
parameters
: 要添加到URL的参数数组 - 对于设置用于设置用户cookie的移动查询标志很有用。 默认: 空数组prefix
: 默认方案。 默认: http://
示例
{# generate mobile url and set a ?mobile=true parameter #}
{{ zenstruck_mobile_url({'mobile': 1}) }}
{# generate full url, set a ?mobile=true parameter and set the prefix to https:// #}
{{ zenstruck_mobile_full_url({'mobile': 0, 'prefix': 'https://'}) }}
建议用法
-
创建3个布局:
full.html.twig
、mobile.html.twig
、layout.html.twig
。 -
将以下内容添加到
layout.html.twig
{% extends zenstruck_mobile_is_mobile() ? "MyBundle::mobile.html.twig" : "MyBundle::full.html.twig" %}
-
让所有模板扩展
layout.html.twig
。如果请求是移动请求,则模板将使用mobile.html.twig
进行装饰。
自定义TwigEngine
此包提供了一个自定义twig引擎。它允许您在存在的情况下自动渲染移动模板。
例如
$this->container->get('templating')->render('MyBundle:Default:index.html.twig');
将在全站正常渲染MyBundle:Default:index.html.twig
模板,但在移动请求时,将查找MyBundle:Default:mobile/index.html.twig
。如果找到,将渲染移动模板;如果没有找到,将渲染默认的全站模板。只需在您的全站模板相同的目录下创建一个mobile文件夹,并在其中创建一个同名模板即可。
在您的config.yml中启用/禁用自定义twig引擎
zenstruck_mobile:
use_twig_engine: true/false
完整默认配置
zenstruck_mobile:
mobile: false
use_listener: true
use_helper: true
use_twig_engine: false
full_host: ~ # Required
mobile_host: ~ # Required
参考
处理移动检测和重定向的.htaccess
的可能设置。这也允许移动设备在设置cookie的情况下覆盖重定向。请参阅上面的Twig Helper示例,了解如何设置mobile
参数。
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine On
# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile} !^$
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie} !\mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com%{REQUEST_URI} [R,L]
# Symfony
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
#TODO
- 允许使用不同的模板(mobile/iphone/ipad等)- 我没有用例,但如果有人想这样做,请创建PR
- 允许不同模板有回退(如果没有ipad,则使用iphone,如果没有iphone,则使用mobile)- 再次,我没有用例,但请发送PR