2013-03-09 43 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文件使其包含您的 製作設置