views:

285

answers:

2

Hi there,

I'm using CodeIgniter to develop a new web app, and I'd like to create a mobile version that users get redirect to when they visit it from their phones.

The mobile version of the app should have a different flow, so swapping CSS/HTML files in the code is not an option for me since the mobile version and the web version will handle things differently in their Controllers and Views, while sharing the same Models.

Anyway how I could do this efficiently?

Your help is much appreciated. :)

+1  A: 

Solution (a):

  • Check in a global controller if the user uses a mobile or a desktop client
  • Load controllers based on the client version (controller_default.php / controller_mobile.php)
  • Do all the client-specific stuff twice
  • Views can be stored in different folders

Note: (a) becomes messy if your application grows over the time.

Solution (b):

  • Modify index.php to load different application folders based on the client used
  • Store your models/configs/libs in a shared folder

Any of the above required (extensive?) modification of the CI framework. Try to do as much as possible by overwriting the existing classes (MY_Controller etc.) to be able to update at a later time.

Philipp
Both are possible, I prefer solution b for medium to large website (say over 10 controller methods). It actually require framework modification but these are very limited if you're overloading CI core classes.
Benoit
+1  A: 

If you really need 2 independant apps, you can setup this with juste one or two codeigniter core modification, depending if you're using PHP4 or 5.

We've done it following this CI's wiki page and it works great, we share models, libraries, configuration. Basically you got a project organization like this :

/application/
   /common/<similar to application directory, but used for shared libs/helper/models/configs files>
   /frontend/<similar to application directory>
   /backend/<similar to application directory>
/system/

By overriding loaders you can implement loading priority if the same lib/config is present for common classes or app-specific). Also you can move all common code (controllers, specific routing class, etc.) and keep only application specific overloadings in your apps.

In the end you got two Front Controllers (mostly identical to the index.php file) one for each app, and you're free to filter visitors with url rewriting, specific subdomain, etc.

If you're targeting servers running PHP4, I opened this thread on codeigniter forums to see what to change in core classes to get it to work (without modification there is a loading issue)

Another viable alternative, but I haven't used it yet, is using HMVC organisation

Benoit
Great! sounds just like what I want, thanks. However, I have no idea how to load a model from 'common' in one of the controllers in 'frontend' or 'backend' folders. How is that done?
KeyStroke
I thought about it last night, that's the missing point in my answer, you have to overrice CI_Loader class so ti will search for model/helpers/libraries/config in Common path before App path.In our setup the APPPATH constant is modified in the front controller and we added a COMMONPATH in the same way.
Benoit
Thanks. One last small bit: how would you recommend switching between the apps if they're supposed to use the same URLs? using agent detection in index.php and then setting a cookie? or another way?
KeyStroke
the cookie is a good way, but I think it's best to redirect visitor to a specific subdomain, especially if Urls do not show the same content and SEO is important to you. Another good point for mobile version is to keep ability for the user to choose between 2 versions. In recent webphones lik HTC HD2 you get a 800*480px screen resolution, more closer to a PC screen than an old 320x240 smartphone. Sometimes i'd rather choose the "normal" version af a website if available
Benoit