Forums / Developer / how to select a siteaccess if we detect a mobile browser?

how to select a siteaccess if we detect a mobile browser?

Author Message

marc marc

Wednesday 23 March 2011 7:13:11 am

hello Community,

I'm working on a mobile version of an existing ezpublish website, I looked all over the web and found nothing on how to do this with ez publish. i have 2 simple questions but may be difficult to answer:
how can ezpublish detect a mobile browser ?
and
how to select a siteaccess if the browser is from a mobile phone?
i thank you in advance for any hints you give me.

Regards,

Marc

Philippe VINCENT-ROYOL

Wednesday 23 March 2011 7:48:37 am

Hi Marc,

An idea is to hack index.php. If you detect mobile phone redirect to http://mobile.example.com which use mobile siteaccess :) Of course dont forget to check url before redirecting ;)

Cheers

Phil

Certified Developer (4.1): http://auth.ez.no/certification/verify/272607
Certified Developer (4.4): http://auth.ez.no/certification/verify/377321

G+ : http://plus.tl/dspe
Twitter : http://twitter.com/dspe

Damien Pobel

Wednesday 23 March 2011 3:05:45 pm

Hi,

To avoid hacking the CMS, you can also create the config.php file at the root of eZ Publish and put your logic to redirect to a mobile specific site there. config.php is included by at the beginning of index.php if it exists.

Cheers

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

H-Works Agency

Thursday 24 March 2011 1:09:09 am

Hello,

Both above solutions induce distrib sources modification. What i would do is :

- Create a custom operator "handle_mobile()" included in pagelayout.tpl

- Put the brain in "extension/$ext_name/autoloads/handle_mobile.php"

I can send you an example of code if you want.

EZP is Great

Marko Žmak

Thursday 24 March 2011 1:21:34 am

"

Both above solutions induce distrib sources modification. What i would do is :

- Create a custom operator "handle_mobile()" included in pagelayout.tpl

- Put the brain in "extension/$ext_name/autoloads/handle_mobile.php"

I can send you an example of code if you want.

"

Yes, this would be the best way to do it. No hacking and a clean eZP way to solve the proeblem.

Also, it could be done using the apache rewrite rule which is maybe easier to implement and would eliminate some overhead. But I can't say for sure that it's the safest bet...

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Damien Pobel

Thursday 24 March 2011 11:38:55 am

Creating a config.php and putting some code in it is not hacking, the purpose of this file is to configure your eZ Publish, redirecting to a specific URL in some cases is a kind of configuration. And IMHO, if doing the same thing in a template operator can perfectly work (as soon as you use it in the pagelayout), it's not the purpose of a template operator.

Doing this in rewrite rules is also possible and a good way but it's usually a bit painful for the average PHP developers.

Cheers

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

Marko Žmak

Thursday 24 March 2011 10:49:30 pm

"

Creating a config.php and putting some code in it is not hacking, the purpose of this file is to configure your eZ Publish, redirecting to a specific URL in some cases is a kind of configuration.

"

Yes, I tought about it too... The distro includes config.php-RECOMMENDED so config.php can be changed without fear. But in my opinion redirecting to a specific URL is not config, but rather programming logic, so theoretically it shouldn't be done in config.php

"

And IMHO, if doing the same thing in a template operator can perfectly work (as soon as you use it in the pagelayout), it's not the purpose of a template operator.

"

There's another issue with the template operator solution. It has to be called in pagelayout, and in eZP the module is executed before parsing pagelayout.tpl, so the redirection will be done only after the execution of module which will degrade performance. For example, for the frontpage first the full node view will be executed and only after that the redirection will be done - which will lead to the mobile version of the forntpage which will again execute the full node view. So allow me to correct myself, this is not the right solution either.

Furthermore, both solution wont work when using static cache (or Varnish), so they're not the right way to do it...

Which leads me to the conclusion that using apache rewrite rules is the right solution. The only thing in question with rewrite rules is wether the detection can be done properly for all mobile devices. If it's something that depends only on the User-agent string, then it can be done using rewrite rules. But if it requires some more complex logic, then we should look for another solution.

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Marko Žmak

Thursday 24 March 2011 10:53:45 pm

Hm, here's another idea...

Set the rewrite rules so that requests are directed to a mobile detection script (let's say detectdevice.php) which then detects the the browser and sends a http redirect to the appropriate URL.

The only drawback is that it will duplicate the number of requests since we do the redirection for every request...

Any toughts about it?

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

André R.

Friday 25 March 2011 2:36:55 am

Doing redirections in templates is really not the cleanest solution by far:

  • It places business logic in the view layer
  • It might not work because of caching or even worse break caching as it never gets to store the result (this is true if it´s inside a cache-block or a node template)
  • It´s at the very end of the request, meaning it will load all of ez publish and perform potential updates on db before it does the redirect

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Marko Žmak

Friday 25 March 2011 5:32:12 am

"

Doing redirections in templates is really not the cleanest solution by far:

  • It places business logic in the view layer
  • It might not work because of caching or even worse break caching as it never gets to store the result (this is true if it´s inside a cache-block or a node template)
  • It´s at the very end of the request, meaning it will load all of ez publish and perform potential updates on db before it does the redirect
"

Yes, after some rethinking I came to the same conclusions... The apache rewrite solution seems the best for now.

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Gaetano Giunta

Friday 25 March 2011 8:57:18 am

You normally want the end user to have a link to go to the 'real' website even when detected he is on mobile (thus you need to set him a cookie).

That, and the fact that detection might be untrivial make me lean towards using config.php

Curious to see how it works out when implemented by the author of the question...

Principal Consultant International Business
Member of the Community Project Board