2013-03-09 44 views
3

我不完全确定如何正确地说这个,所以我提前道歉。我有一个稍微独特的设置,但同时又不是那么独特。我想有Codeigniter一个服务与多个接入点,子域,htaccess

api.domain.com 
m.domain.com 
domain.com 

所有工作从相同的代码库,但服务了不同的意见,和工作过不同的控制器套。但是我不想通过在特定于子域​​自身的各种目录中创建镜像副本来复制我的代码库。对我而言,这是多余的,而且与生产率相反,因为我将不得不管理3套以上的模型,库以及某些情况下的控制器。维护各种版本的服务的功能。

现在,我设置和工作的是通过routes.php的不断增长来说明通过普通域时使用什么控制器。

domain.com 
domain.com/m/ 
domains.com/api/ 

这对于现在的工作,但我试图想什么最好的组织和服务的未来发展。

所以在我所有的问题是,我如何设置codeigniter支持使用子域的逻辑,同时保持在一个主要代码库的一切。这是否合理?如果是这样,怎么能实现呢?

+0

可能的重复:http://stackoverflow.com/questions/7085670/subdomain-based-on-codeigniter-controller-name – 2013-03-09 05:32:58

+0

不完全是我正在寻找的解决方案,但是让我在盒子外面想一点只要它想出我自己的变化。 – chris 2013-03-09 06:52:43

回答

2

好吧,所以在对我原来的帖子发表评论后,指着我在另一个帖子里,我提出了一个处理我的问题的漂亮方法。它不完全是链接中发现的答案,而是根据逻辑推导出来的。由于我有多个子域,因此我想分别使用自己的一组功能和需求以及特定于其原因的控制器,这些控制器只应从这些子域中调用。

这就是说我的解决方案,对于那些谁可能偶然发现是,在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 */ 

直线心中也请我确实想为每个子域

0

我默认和404控制器假设您可以根据ENVIRONMENT常量加载不同的配置。

http://ellislab.com/codeigniter/user-guide/libraries/config.html

您可以加载根据当前 环境不同的配置文件。环境常量在index.php中定义, 在处理环境部分有详细描述。

要.PHP

例如创建特定于环境的配置文件,创建或在应用程序/配置/ {环境}复制 配置文件/ {FILENAME},以创建生产仅配置。PHP,你会:

创建目录的application/config /生产/现有 config.php文件复制到上面的目录编辑 的application/config /生产/ config.php文件使其包含您的 制作设置