4

What I want to do is to create a mobile version of my web site in CodeIgniter.

I want to redirect my complete web site to m.example.com

There will be no change in controllers, neither in views and models. Both will be the same.

I don't want to change my .htaccess file. Any possible solutions for this?

Onur
  • 414
  • 1
  • 11
  • 31
  • 1
    Check for mobile detection in controller and redirect it. Pretty straight forward in my opinion. What issue you are having? – itachi Dec 18 '12 at 08:04
  • If there is no change in controllers, models, or views why do you need a mobile site? – Rooneyl Dec 18 '12 at 08:06
  • To let Google index mobile domains for now. I will seperate them later. – Onur Dec 18 '12 at 08:08
  • itachi, am I supposed to check mobile control in every controller? Any simple solution in general like routes ...etc? – Onur Dec 18 '12 at 08:10
  • @OnurGöker if you want to do this in every controller just extend the base CI controller. – Rooneyl Dec 18 '12 at 08:27
  • @Rooneyl ok I already extend CI_Controller, but where will I redirect? – Onur Dec 18 '12 at 08:44
  • @OnurGöker, wherever you want. If mobile the redirect to your mobile site, adding the querystring. – Rooneyl Dec 18 '12 at 09:03

4 Answers4

9

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.

Rooneyl
  • 7,802
  • 6
  • 52
  • 81
6

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 :) !

Dom
  • 1,018
  • 2
  • 11
  • 20
  • +1: in my opinion, this solution is perfect and simple. Only change the views, keep all other stuff as it is. – butters Sep 27 '13 at 20:42
  • I used a very similar solution to this. The only different was because I didn't need to change every view to mobile, I would do a file_exists check first, falling back to the desktop version if it didn't – Richard Merchant Jan 06 '14 at 13:05
3

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/
0

Ok I found another solution. I used hooks pre controller and I redirect www subdomain to m subdomain.

Onur
  • 414
  • 1
  • 11
  • 31
  • can you share me this solution, I have change only view and controller and model same for desktop and mobile site. – Chirayu Vyas Feb 25 '20 at 05:30
  • @ChirayuVyas, it would be better you check for hooks in CodeIgniter: https://codeigniter.com/user_guide/general/hooks.html – Onur Feb 28 '20 at 17:34