tags:

views:

19

answers:

1

I'm not sure I'm going down the right path with this - and I am pretty new to routing, so if this is way off base, I apologize.

What I'm trying to accomplish is having a link at the bottom of the page where the site visitor can select mobile or full screen. I want them to be able to do this from any page. When they select either one, there could be any number of things happen - depending on where they are on the site (i.e. different views, functionality, etc).

Right now, I don't have Codeigniter configured to allow querystrings, which is the default behavior. So in order to pass a preference in a link, I need to do it using a url segment.

I was hoping to do something like:

<? if ($in_mobile_view): ?>
  Mobile | <a href="<?= current_url() ?>/f">Full Site</a>
<? else: ?>
  <a href="<?= current_url() ?>/m">Mobile</a> | Full Site
<? endif ?>

This works great when I am navigating to: /welcome/index/m or /calendar/view/m, etc. However, if I'm just at: /welcome/m or /m - where the index method of the controller should kick in, I get a 404 because it can't find the method - since it doesn't exist.

My thought was that via routing, I could configure Codeigniter to ignore the "m" and "f" strings and just operate as if they aren't in the url at all.

Is that a good way to go about this? If not, I'd love to hear other suggestions. If this is a decent way to go, I'd really appreciate if someone could point me in the right direction for routing.

Thanks for your time.

+1  A: 

Why select this with the URL?

You can detect basic "is mobile" with the User Agent library. Take that value and set the session to whichever the User Agent suggests, then simply link to a controller that switches the user between mobile and full versions when clicked.

Run this in global code somewhere as a hook or a Base Controller.

if($this->user_agent->is_mobile() && ! $this->session->userdata('site_mode'))
{
    $this->session->set_userdata('site_mode', $this->user_agent->is_mobile() ? 'mobile' : 'full');
}

Then you controller can just set the session to whatever based on what they have clicked.

/switch/mobile
/switch/full

Phil Sturgeon
Thanks Phil. I do have the detection code in place. My goal was to give the visitor the option to select the other view. So if someone with an iPhone is visiting the page, they will be automatically be given the mobile experience. However, they will also be given the option to select the full experience if that's what they prefer. I'll store that choice in a cookie, so the next time the visitor comes, they'll be shown whatever display choice they made. My only question with your solution is how I can redirect people back to the page they came from if I link to /switch/mobile or /switch/full
someoneinomaha
Wel you didn't make that very clear :-plink to switch/mobile/controller/method/whatever and have...function mobile(){ // whatever cookie stuff $uri = redirect($uri);}That will send it back to controller/method/whatever.
Phil Sturgeon