eZ Community » Forums » Developer » how to select a siteaccess if we...
expandshrink

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

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

Wednesday 23 March 2011 3:13:11 pm - 16 replies

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

Wednesday 23 March 2011 3:48:37 pm

Hi Marc,

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

Cheers

Phil

Wednesday 23 March 2011 11: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

Thursday 24 March 2011 9: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.

Modified on Thursday 24 March 2011 9:13:13 am by H-Works Agency

Thursday 24 March 2011 9: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...

Thursday 24 March 2011 7:38:55 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. 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

Modified on Thursday 24 March 2011 7:40:27 pm by Damien Pobel

Friday 25 March 2011 6:49:30 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.

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.

Friday 25 March 2011 6:53:45 am

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?

Friday 25 March 2011 10: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

Friday 25 March 2011 1:32:12 pm

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.

Friday 25 March 2011 4:57:18 pm

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...

Modified on Friday 25 March 2011 4:57:48 pm by Gaetano Giunta

Thursday 01 December 2011 4:45:41 pm

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.

Hello,

I'm interessed by your example code. Can you send me it ?

Thanks in advance

Djoo

Thursday 01 December 2011 5:06:43 pm

Hello,

I was talking of an example structure for an ezpublish template operator. I don't have a specific operator to detect mobile browser.

Here is the php code that would link {parsedate()} in pagelayout.tpl to executing the code in extension/$ext_name/autoloads/ezparsedate.php :

 class eZParseDate{
 
function eZParseDate() {
}
function operatorList() {
return array( 'parsedate' );
}
function namedParameterPerOperator() {
return true;
}
function namedParameterList(){
 
return array( 'parsedate' => array( 'params' => array( 'type' => 'string',
 'required' => false,
'default' => array() ) ) );
}
 
function modify( $tpl, $operatorName, $operatorParameters, &$rootNamespace, &$currentNamespace, &$operatorValue, &$namedParameters )
{switch ( $operatorName ) {
case 'parsedate': {
$operatorValue = '8 of july';
}
break;
}
}}

Don't forget to add operators settings in extension/$ext_name/settings/template.ini

Modified on Thursday 01 December 2011 5:10:21 pm by H-Works Agency

Thursday 01 December 2011 7:34:55 pm

Hello Johann Roche,

You -really- -really- -really- need to stop what your doing.

You need to listen to the powerful and correct warning's André R is sharing ...

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

 

What your trying to do is a very bad idea in practice and more specifically the wrong way to provide the feature your trying to achieve regarding client browser detection and siteaccess redirection.

 

I'm sorry to say this but: What you seem to hell bent / insisting on trying to do / how your trying to do this ... is basically (these days) just about the dumbest way to achive your goal.

 

You should really be trying to use Damien Pobel's suggestion.

It was the best I've seen in this thread so far.

Put your browser detection / redirection code in the config.php file using PHP directly.

Use eZ Publish correctly!

 

Please! Listen to the masters here trying to teach you how to do this correctly.

Otherwise why did you even bother asking for help if you were not going to really listen to what was shared ...

 

Apologies for my tone, I only seek to save the lost souls from standing so close to the edge of oblivion ...

 

Cheers,

Heath

Modified on Thursday 01 December 2011 7:35:22 pm by // Heath

Sunday 04 December 2011 5:14:50 pm

Some thought about this one...

One thing that's in my mind for quite a long time is: "What is really business logic and what is the presentation layer?". When we use the fetch function in the templates isn't also some business logic there? Also, with all the functions and operators we have in eZP templating language, I believe we're talking about something quite more powerfull than just a presentation layer. To conclude I believe there's no clean distinction (at least in eZP) between business logic and presentation layer.

Second, what if we have a case where you need to enable the editor to control from the admin site if some page should redirect to another url, or not? (I have encountered many such situations in my experience) So let's say this is controlled via an attribute of an object. In this case should we really make the trouble of implementing business logic in extension PHP files while it's far more easier to do it in templates via a redirect operator?

I'm not saying that a template operator is the right solution for marc's question, but that there are some cases where it is a better one.

Modified on Sunday 04 December 2011 5:24:10 pm by Mavko Žmak - Žmale

Friday 15 March 2013 11:19:48 am

I need to do this too but i can't find the actual answer in this thread...

Please can anyone tell me what code to put in the config.php file to make it redirect to my "mobile" siteaccess when it detects a mobile browser/device?

Thanks in advance happy.gif Emoticon

Sunday 17 March 2013 6:18:29 pm

Ok, i found this bit of code to put in the htaccess

RewriteCond %{REQUEST_URI} !^/iphone/.*$
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
    RewriteRule ^(.*)$ /iphone/ [L,R=302]

however, when i use this on an ezflow install it repeats over and over in the url

Please can someone assist?

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from