我不太确定如何恰当地表达这一点,所以我提前道歉。我有一个稍微独特的设置,但同时也不是那么独特。我想要
api.domain.com
m.domain.com
domain.com它们都使用相同的代码库,但提供不同的视图,并且使用不同的控制器集。但是,我不想通过在特定于子域本身的各种目录中制作代码库的镜像副本来复制代码库。对我来说,这是多余的,而且是生产力的反面,因为我必须管理3+模型集、库,在某些情况下还需要管理控制器。以跨服务的不同版本维护功能。
现在,我的设置和工作是通过不断增长的routes.php来说明当通过一个正常的域时,使用了什么控制器。
即
domain.com
domain.com/m/
domains.com/api/这在目前是可行的,但我正在努力思考什么对组织和未来的服务发展是最好的。
所以我的所有问题是,我如何设置codeigniter来支持使用子域的逻辑,同时将所有内容都保存在一个主代码库中。这是合理的吗?如果是这样的话,如何实现呢?
发布于 2013-03-09 15:02:27
好了,在对我的原始帖子进行了评论之后,我想出了一个很好的方法来处理我的问题。这不完全是在链接中找到的答案,而是基于逻辑的导数。因为我有多个子域,所以我想推出每个子域都有自己的一组功能和需求,以及只应从这些子域调用的特定于其原因的控制器。
也就是说,对于那些可能会遇到它的人,我的解决方案是,在routes.php中,我最终做了一个小函数,让HTTP_HOST基于.拆分它,并从那里使用它来满足我的需求。我的例子如下。
请注意,我还替换了routes.php中的所有内容,因此它不仅仅是$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 */还请记住,我确实希望每个子域的默认和404控制器
发布于 2013-09-16 00:01:56
我想你可以根据环境常量加载不同的配置。
http://ellislab.com/codeigniter/user-guide/libraries/config.html
您可以根据当前环境加载不同的配置文件。环境常量在index.php中定义,并在处理环境一节中进行了详细描述。
要创建特定于环境的配置文件,请在application/config/{ENVIRONMENT}/{FILENAME}.php中创建或复制配置文件
例如,要创建仅用于生产的config.php,您需要:
创建目录application/config/ production /将现有的config.php复制到上面的目录中编辑application/config/production/config.php,使其包含您的生产设置
https://stackoverflow.com/questions/15307518
复制相似问题