BsMobilePlugin is a project mainly written in PHP, it's free.
Detect the mobile handset by its user agent and redirect to the provided mobile site.
Detect the mobile handset by its user agent and redirect to the provided mobile site. This is the code from this blog post in an easy to install plugin: Blog post
Install from the project root.
For a git project
$ git submodule add bsgitexternal:beeldspraak/sf/bsMobilePlugin plugins/bsMobilePlugin
For other projects, optionally add ".git" to ignore from the version control system
$ git clone bsgitexternal:beeldspraak/sf/bsMobilePlugin plugins/bsMobilePlugin
enable the plugin in config/ProjectConfiguration.class.php
# apps/front/config/filter.yml
redirectUserAgent:
class: redirectUserAgentFilter
enabled: on
param:
redirectUrl: http://mobile.yourdomain.com
userAgents:
- android, mobile # android mobile, seperate multiple words in the user agent by a comma
- android # android tablet
- iphone
- ipod
- ipad
- blackberry
# prevent a mobile device with the configured userAgents to be redirected
stopParameters:
- no_mobile
stopRoutes:
- project_api
- recent_api
# ...
# apps/front/config/filter.yml
redirectUserAgent:
class: redirectUserAgentDiemFilter
enabled: on
param:
redirectUrl: http://mobile.yourdomain.com
userAgents:
- android, mobile # android mobile, seperate multiple words in the user agent by a comma
- android # android tablet
- iphone
- ipod
- ipad
- blackberry
# prevent a mobile device with the configured userAgents to be redirected
stopParameters:
- no_mobile
stopRoutes:
- project_api
- recent_api
# map the current route for the given module and actions and add the url to the redirectUrl, replace parameters
mapModules:
project/show:
url: '#work/show/:id'
params: { id: ':id' } # get the record 'id' to replace ':id'
news_post/show:
url: '#recent/show/:id'
params: { id: ':id' }
blog_post/show:
url: '#recent/show/:id'
params: { id: ':id' }
main/contact: { url: '#contact/index' }
# ...
The user object is extended with some methods, see lib/MobileUser.class.php:
Use it for example in your template to display a button to go back to the mobile site:
<!-- apps/front/main/templates/_header.php -->
<?php if ($sf_user->isMobileDevice()): ?>
<div id="mobile_site">
<p><a href="<?php echo url_for('@mobile_site') ?>">Mobile site</a></p>
</div>
<?php endif; ?>
<!-- ... -->
// ...
public function executeMobileSite(sfWebrequest $request)
{
// remove from session
$this->getUser()->resetRedirectStop();
$this->redirect('http://mobile.yourdomain.com');
}
// ...