CodeIgniter Forums
default controller route for mobile - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: default controller route for mobile (/thread-63153.html)



default controller route for mobile - nplaviola - 09-30-2015

My site that uses a mobile subfolder in my controllers and views directories to serve up a mobile friendly version when a user logs in from a phone. The login controller makes that determination when the user logs in, and utilizes the appropriate controllers and view. That works fine. However, when a user navigates away from the site and then attempts to hit mysite.com again, the $route['default_controller'] sends them to 'home', which is the normal version of the site. Is it possible to have a conditional statement in the routes.php file that checks if the user is on mobile and then sets the default_controller, or is there a better way to approach this problem? Thanks!


RE: default controller route for mobile - slax0r - 09-30-2015

This sounds wrong. To do a mobile version you want only one view, for mobile, and desktop, and not multiple views. You then use stylesheets to style the page differently for mobile and desktop.


RE: default controller route for mobile - JayAdra - 10-01-2015

Not necessarily. While it is good to have a responsive site (which is styled differently for different devices/screen sizes), you can also have a separate view completely for the mobile site (if it's drastically different).

However, you should be doing this either by having a separate mobile subdomain or similar (m.example.com) or by detecting device on the server side (if you can reliably) and serving the appropriate view (but still using the exact same routes).

It really depends on your requirements.


RE: default controller route for mobile - nplaviola - 10-01-2015

The mobile site is drastically different and only has limited functionality compared to the full site. I ended up modifying my 'home' controller to detect the device the user is on and then redirect them to the 'homemobile' controller if necessary. I also ended up adding a method to force the mobile site into desktop view if that's what the user wants to do, which was the catalyst for this issue.

FYI I ended up using the php script from here:

http://detectmobilebrowsers.com

And it works brilliantly.

Thanks for the replies.


RE: default controller route for mobile - slax0r - 10-01-2015

If you have chosen to go this path, then I would advise you move your "mobile detect" piece of code into your base controller(MY_Controller), and have all your controllers extend from it. This way no matter to which URL the user will navigate, your system will check for a mobile device and redirect accordingly.


RE: default controller route for mobile - mwhitney - 10-05-2015

While I'm not a big fan of redirecting mobile browsers and limited functionality on mobile, CI's User_agent library should be perfectly capable of detecting mobile browsers.

PHP Code:
if ($this->agent->is_mobile())

{
    $this->load->view('mobile/home');
}
else
{
    $this->load->view('web/home');