Codeigniter一个服务具有多个接入点,子域,htaccess [英] Codeigniter one service with multiple access points, subdomains, htaccess

查看:125
本文介绍了Codeigniter一个服务具有多个接入点,子域,htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何正确地说这个,所以我提前道歉。我有一个稍微独特的设置,但在同一时间不是那么独特。我想拥有

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

所有使用相同的代码库,但提供不同的视图,但是,我不想复制我的代码库,通过镜像副本在各个目录特定于子域​​本身。对我来说,这是多余的,与生产力的相反,因为我将不得不管理3+套模型,库,在某些情况下控制器。保持各种版本的服务的功能。

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.

现在,我已经设置和工作是通过不断增长的routes.php是一种手段,说控制器在通过正常域时使用。

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.

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?

推荐答案

好的,所以在对我的原始帖子发表评论后,指向我另一个帖子在这里堆栈我想出了一个漂亮的方式来处理我的问题。它不完全是在链接中找到的答案,而不是基于逻辑的派生。由于我有多个子域,我想推出每个具有自己的一套功能和需求,以及特定于其原因的控制器,只应该从这些子域中调用。

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.

这说明我的解决方案,对于那些可能绊倒它是在 routes.php 我结束了一个小功能,以获得 HTTP_HOST 根据分割它。并根据我的需要使用它。我的例子如下。

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.

请记住,我也替换了routes.php中的所有内容,所以它不仅仅是一条 $ route [ this / that'] ='dir / controller';

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 */

还要记住,我想要每个子域的默认和404控制器

Also keep in mind I do want default and 404 controllers for each subdomain

这篇关于Codeigniter一个服务具有多个接入点,子域,htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆