开发者

Share CI models between different applications

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 sub开发者_开发知识库domain 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!


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜