6

I'm still trying to figure out a way to get a mobile site running. I have a web app in CodeIgniter and I would like to create a mobile version of it. However, I don't want to rewrite too much of my code, especially not my models.. (Since they return raw data which I can use anywhere.)

I want to run my mobile website at a subdomain (m.mydomain.tld). The desktop version runs at www.mydomain.tld. I tried pointing the subdomain to my CI application folder; when a mobile browser arrives at www.mydomain.tld, I redirect it to m.mydomain.tld. This subdomain is, as I said, pointed at my CI applicaiton folder; I then serve mobile-optimised controllers and views.

However! As specified in app/config/config.php, the base_url of my application is:

$config['base_url'] = 'http://www.mydomain.tld/';

So redirecting to m.mydomain.tld doesn't really work because I still get redirected to www and I don't want that, even though it then does what I want it to do.

The way I'm trying to solve this is making two application folders with different controllers/views, but shared models etc. So, I'm trying to figure out a way to restructure my CodeIgniter application so I can share my models and 'custom controller' (MY_Controller), as well as some custom libraries/helpers between different applications.

I hope this is clear, if not I'll gladly explain more what I'm looking for. Thanks a lot!

Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
  • Add another view to the models for your mobile version rather than creating a whole new application. I have never used CI, but this comes as part of the package with http://www.agavi.org This is after all the whole point of the MVC architecture. – Treffynnon May 12 '11 at 13:09
  • Updating my question with why I don't think this is possible. Thanks! – Joris Ooms May 12 '11 at 13:12
  • Oh I see your issue. You need to make your config file sensitive to the location your application is working in. – Treffynnon May 12 '11 at 13:20

3 Answers3

3

Maybe you should take a look at codeIgniter's 2.0 new feature : package.

Packages allow you to share librairies, models etc. with :

$this->load->add_package_path('/usr/local/codeigniter/shared');

Read this: http://philsturgeon.co.uk/blog/2010/04/codeigniter-packages-modules

Hope it helps.

927PM
  • 31
  • 5
  • Thanks. I'll make sure to check it out, hadn't heard of it to be honest. Sounds like it might be what I need. – Joris Ooms May 12 '11 at 17:19
2

I think the right way to solve this problem is just creating a new set of controllers and views (targeted for mobile) in your existing web app. Then just define routes for new "mobile" controllers (maybe you should also add a "m/" or "mobile/" prefix for each of the routes), and that's all.

Update:

Ok, I see. I recommend you to do this way:

  1. First create this record in config.php:

    $config["mobile_base_url"] = "http://m.yoursite.tld/";
    
  2. Then create your url_helper, add a mobile_site_url() function, which would be an analogue to CI site_url() function (you will need this for links in views and controllers).

  3. Create mobile views and controllers, define routes for them with "m/" prefix.

  4. Do a little .htaccess hack for url rewriting described at this forum page http://www.webmasterworld.com/apache/3509887.htm, so that all requests to m.yoursite.tld/... would go to yoursite.tld/m/...

I guess that you will run in some issues with this approach, but you should definitely not create separate CI app for your problem.

Hope this helps.

Misha Karpenko
  • 2,168
  • 17
  • 18
  • You shouldn't need a whole new set of controllers just new views. The controller would just have an additional method added to it for mobile to point to the correct view. Again I don't know if CI really is MVC though. – Treffynnon May 12 '11 at 13:14
  • I agree with you. I've recommended creating controllers in case if some data would be unneeded in mobile views, so creating optimized controllers would make less load for models. – Misha Karpenko May 12 '11 at 13:19
  • I don't think CI is NOT MVC, it just doesn't care how you code. If your developers don't typically work in MVC, your CI code will NOT end up looking anything like MVC. – rich remer Aug 28 '13 at 00:13
2

In config.php replace $config['base_url'] = 'http://www.mydomain.tld/'; with this:

if(isset($_SERVER['HTTPS']) and 'on' === $_SERVER['HTTPS']) {
    $config['base_url'] = 'https://';
} else {
    $config['base_url'] = 'http://';
}
$config['base_url'] .= $_SERVER['SERVER_NAME'] . '/';

That will obtain the URL from the Apache/server environment variables so if you access the site via the subdomain then it will use the subdomain URL or if you access it via the root domain it will use the root domain.

I am not a CI expert so there maybe other ways, but this is the simplest solution I know of.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • This is actually what the latest CI versions do to (finally) guess the base url if it isn't set. (Although it's slightly different) – Wesley Murch May 12 '11 at 16:16