Mobile version of site using CodeIgniter Mobile version of site using CodeIgniter codeigniter codeigniter

Mobile version of site using CodeIgniter


The user agent class has a function;

$this->agent->is_mobile();

You could use this in the construct of your base controller(s) to test if mobile.


Instead of rewriting your code all over the place, you can just load a different view folder. In essence what will happen is you can just have CodeIgniter load a different view with the same name from another folder everytime you use $this->load->view("xxx"). First, create a new folder in your view folder called /mobile and just create views with the same exact naming conventions and it will load the the view accordingly by extending the Loader.php class.

Whether you are doing a responsive design or if you are going to create an iPhone app looking mobile version of your site, kind of like what facebook does, then you can just override the Loader class in the core folder. In your application/core folder create a MY_Loader.php and put it in that file.

Mine looks like the following

<?php  if (! defined('BASEPATH')) exit('No direct script access allowed');class MY_Loader extends CI_Loader{    //overides existing view function        function view($view, $vars = array(), $return = FALSE)    {        $CI =& get_instance();        $CI->load->library("user_agent");        if($CI->agent->is_mobile()){            $view = 'mobile/'.$view;        }        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));    }}  ?>

Responsive web design is a mess in my opinion, but this still separates the code for you very nicely, while still being able to use your controllers and models in unison.

Hope this helps. This is the way I will be going about it :) !


Why a redirection? If everything is the same, why not look into Responsive webdesign?

24ways.org has some good articles for it:

http://24ways.org/2012/responsive-responsive-design/http://24ways.org/2012/responsive-images-what-we-thought-we-needed/