• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter with multiple subdomains

#1
Hi to all members,
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
Reply

#2
As per your requirement you need to added a unique field for subdomain. And another option for you, you can make multiple tables as per subdomain for manage the content as per subdomain.

That said my solution, for those who may stumble across it is, in the routes.php I ended up making a small function to get the HTTP_HOST split it up based on . and use it from there to my needs. My example is as follows.

Mind you I also replaced everything in the routes.php so its not just a straight line of $route['this/that'] = 'dir/controller';

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|   example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|   http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
|   $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|   $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/
function whichSubRoute()
{
    $subs = array(
                "api"=>"api/",
                "m"=>"m/"
                );

    $curr = $_SERVER['HTTP_HOST'];
    $curr = explode('.', $curr);
    if(array_key_exists($curr[0], $subs))
    {
        return array($curr[0], $subs[$curr[0]]);
    }
    return false;
}

//due to the the way this setup works, some controller references
//can be found multiple times (and in no particular order).
//also note due to this setup, each method has its own default and 404
$choiceRoute = whichSubRoute();
if($choiceRoute !== false)
{
    if($choiceRoute[0]=="api")
    {
        $route['default_controller'] = "welcome";
        $route['404_override'] = '';
        //start version 1 (mvp API)
        $route['1.0/user/(:any)'] = $choiceRoute[1].'v1_userinfo/index/$1';
        //controllers outside of "/api"
    }
    if($choiceRoute[0]=="m")
    {
        $route['default_controller'] = "welcome";
        $route['404_override'] = '';
        //start version 1 (mobile)
        $route['welcome']                   = $choiceRoute[1].'m_welcome';
        $route['dashboard']                 = $choiceRoute[1].'m_dashboard';
        $route['user/(:any)']               = $choiceRoute[1].'m_userinfo/index/$1';
        $route['reg']                       =
        //controllers outside of "/m"
        $route['login/auth']                = 'login/auth';
        $route['logout/mobile']             = 'logout/mobile';
        //end version 1 (mobile)
    }
}
else
{
    $route['default_controller'] = "welcome";
    $route['404_override'] = '';
}
/* End of file routes.php */
/* Location: ./application/config/routes.php */
Reply

#3
(01-02-2018, 12:09 AM)Thanks for your reply,In database I have a separate field for each country to get data for specific country. I understand the routing modification. you are getting the host and then split it by fining "." in the name of the host and the results stored in an array. so at the 0 index of the array you have sub-domain name. But I\m confused about how to upload application on server. for each sub-domain I need to upload this application. Infect I Wrote:
As per your requirement you need to added a unique field for subdomain. And another option for you, you can make multiple tables as per subdomain for manage the content as per subdomain.

That said my solution, for those who may stumble across it is, in the routes.php I ended up making a small function to get the HTTP_HOST split it up based on . and use it from there to my needs. My example is as follows.

Mind you I also replaced everything in the routes.php so its not just a straight line of $route['this/that'] = 'dir/controller';

[code redacted]
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.