问题
I am not exactly sure how to word this properly so I apologize in advance. I have a slightly unique setup, but at the same time not so unique. I want to have
api.domain.com
m.domain.com
domain.com
All working off the same codebase, but serving up different views, and working off different controller sets. However I do not want to duplicate my code base by making mirror copies of it in various directories specific to the sub domain itself. To me that is redundant, and the opposite of productive, since I would have to manage 3+ sets of models, libraries, and in some cases controllers. To maintain functionality across the various versions of the service.
Right now, what I have setup and working is through constant growth of the routes.php is a means of saying what controller is used when through a normal domain.
ie
domain.com
domain.com/m/
domains.com/api/
Which works for now, but I am trying to think of whats best for organization and future development of the service.
So in all my question is, how can I setup codeigniter to support this logic of using subdomains while keeping everything in one main code base. Is this plausible? If so how could it be achieved?
回答1:
Ok, so after a comment made to my original post, pointing me to another post here on stack I came up with a nifty way of handling my issue. Its not exactly the answer found in the link more than a derivative there of based on the logic. As I have multiple sub domains I want to roll out each with its own set of functionality and needs, as well as controllers specific to its cause that should only be called from those subdomains.
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';
<?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 */
Also keep in mind I do want default and 404 controllers for each subdomain
回答2:
I suppose you can load different configs base on ENVIRONMENT constant.
http://ellislab.com/codeigniter/user-guide/libraries/config.html
You may load different configuration files depending on the current environment. The ENVIRONMENT constant is defined in index.php, and is described in detail in the Handling Environments section.
To create an environment-specific configuration file, create or copy a configuration file in application/config/{ENVIRONMENT}/{FILENAME}.php
For example, to create a production-only config.php, you would:
Create the directory application/config/production/ Copy your existing config.php into the above directory Edit application/config/production/config.php so it contains your production settings
来源:https://stackoverflow.com/questions/15307518/codeigniter-one-service-with-multiple-access-points-subdomains-htaccess