views:

103

answers:

4

I am planning for a web application. I am using the PHP framework Kohana with Smarty. My web application will also have mobile device interface. Now, there will be lot of common and lot of separate code for the two interfaces.

How should I organize the the code so that:

  1. There is no duplication of code.
  2. Unnecessary code does not get loaded into. For example, the desktop UI specific code should not get loaded into iPhone interface or vice versa.
+1  A: 

One way would be to simply use separate views for desktop browsers and mobile browsers. All your logic will be in your controller so you wouldn't be duplicating code, you would just call the relevant view based on the user-agent string of the browser.

jackbot
+4  A: 

You have several choices really!

You could choose to go down the "2 project" route using shared Kohana modules - but I personally dislike the approach.

I would personally use a similar approach as a multi lingual site - So ... apache (or whatever) would rewrite m.example.tld/my/page -> www.example.tld/mobile/my/page

Assuming your using Kohana3 - The standard route could be changed to something like:

Route::set('messages', '<format>/(<controller>(/<action>(/<id>)', array('format' => '(mobile|desktop))
->defaults(array(
 'format'     => 'desktop',
 'controller' => 'welcome',
 'action'     => 'index',
));

So - Users will never see the /mobile/ URLs, but you can now choose which smarty template based on Request::instance()->param('format');

Likely - You'll only need to duplicate the view files/smarty templates for each platform.

I use a similar pattern for output formats ... XML, JSON, XHTML, RSS ..

Hope this helps ;)

Kiall
A: 

I definitely suggest having separate views for mobile and desktop displays. If at all possible, don't tie the view to the user-agent string directly -- use the user-agent string to direct mobile devices to a separate URL (like m.whatever.com/page, or www.whatever.com/m/page) which renders separately.

Coderer
A: 

Finally I followed the approach of different 2 Project route. Because it gave a lot of flexibility. If I want to share some models, views, controllers I put them in "modules". Else, if I am want to have something separate for two interfaces, I put them in the corresponding project.

So: my source looks like:

</>
  proj_desktop/
    htdocs/
    application/
      models/
      views/
      controllers/
  proj_iphone/
    htdocs/
    application/
      models/
      views/
      controllers/
  proj_mobile/
    htdocs/
    application/
      models/
      views/
      controllers/
  modules/
    myApp/
      models/
      views/
      controllers/
  system/
    models/
    views/
    controllers/
Sabya