0

I have a problem and hopefully we will solve it.

The problem is, I am creating an application this application has an “Admin Panel (Backend)” and Frontend.

The URL structure of the admin panel is like this

https://www.app-name.com/admin

For frontend I have a different URL structure which is based on country for example.

https://usa.app-name.com

https://ca.app-name.com

https://uk.app-name.com

https://uae.app-name.com

https://in.app-name.com

It means in frontend I have multiple sub-domains. And the all these sub-domains are sharing a single database. As well as, backend is also sharing the same database.

Now my problem is how can I handle front-end like this.

Possible one solution in my mind is to copy app to all the sub-domains and connect with single database. And for admin panel copy it at the main domain.

But in this case, if I have a single modification to application I have to update all the copies. I want to use only single copy of app to handle all sub-domains.

Anyone have some solution for this problem. Thanks

1
  • is there a difference between those domains ? Commented Jan 9, 2018 at 13:30

1 Answer 1

1

I'd suggest executing some code in a important model for the app that gets auto-loaded through the entire app. Maybe call it Site_model.php.

Firstly, lets autoload the model. Open up config/autoload.php and add the model into the list of models to be autoloaded:

$autoload['model'] = array('Site_model');

Now, create the new model calls Site_model. In the construct of the model, add some code along these lines (this is a simply basic example of how to fetch and assign the value of a subdomain. I imagine there are better ways, but for this example, it will do):

class Site_model extends CI_Model {
   var $subDomain = '';
   function __construct() {
      $this->subDomain = array_shift((explode('.', $_SERVER['HTTP_HOST'])));

      // further code which uses the value of subDomain to fetch the correct record from the database.
   }

Now, anytime you want to reference the subdomain in your code, you can simply get it like this:

echo $this->Site_model->subDomain;

This should also work for the admin backend. Oh, and ensure that the model is the first to be auto-loaded, in case any of the other models that are auto-loaded initialise code that is dependent on the site model's values.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply. Really I did not understand what you mean from "Important Model". Please help me by just providing me an example of this. I'm very great-full to you. Thanks
@WaqasKhan I've updated my answer with a basic example (not tested). There may be better ways, but this is a simple example which explains one way to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.