someoneinomaha
someoneinomaha

Reputation: 1304

How do you configure Codeigniter routing to ignore a specific segment string in the url

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.

Upvotes: 1

Views: 1255

Answers (1)

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

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

Upvotes: 3

Related Questions