Sharing CI models between different applications

I'm still trying to find a way to launch a mobile site. I have a web application in CodeIgniter, and I would like to create a mobile version of it. However, I do not want to rewrite too much code, especially my models. (Because they return raw data, which I can use anywhere.)

I want to run my mobile site in a subdomain ( m.mydomain.tld ). The desktop version runs www.mydomain.tld . I tried to specify a subdomain in the CI application folder; when the mobile browser arrives at www.mydomain.tld , I redirect it to m.mydomain.tld . This subdomain, as I said, pointed to my CI application folder; Then I serve mobile optimized controllers and views.

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

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

Therefore, redirecting to m.mydomain.tld really does not work, because I am still redirecting to www , and I do not want this, although it does what I want it to do.

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

I hope this is clear, if it is not with pleasure that I explain more about what I am looking for. Many thanks!

+6
source share
3 answers

In config.php replace $config['base_url'] = 'http://www.mydomain.tld/'; in the following way:

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

This will result in the URL from the Apache / server environment variables, so if you access the site through a subdomain, then it will use the URL of the subdomain, or if you access it through the root domain, it will use the root domain.

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

+2
source

Perhaps you should take a look at the new CodeIgniter 2.0 feature: package.

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

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

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

Hope this helps.

+3
source

I believe that the correct solution to this problem is to create a new set of controllers and views (designed for mobile devices) in your existing web application. Then simply define the routes for the new "mobile" controllers (perhaps you should also add the prefix "m /" or "mobile /" for each of the routes) and that’s all.

Update:

Ok, I see. I recommend you do this:

  • First create this entry in config.php file:

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

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

  • Make a little .htaccess to rewrite the url described on this forum page http://www.webmasterworld.com/apache/3509887.htm so that all requests to m. yoursite.tld / ... will go to your site .tld / m / ...

I assume that you will encounter some problems with this approach, but you should not create a separate CI application for your problem.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/887928/


All Articles