子域URL段可以用作Codeigniter中的类吗?

时间:2012-02-29 15:11:31

标签: codeigniter subdomain wildcard-subdomain segments

我需要使用子域作为类。即,而不是:

www.example.com/class/function/ID,例如www.example.com/town-name/history/1

我需要

class.example.com/function/ID,例如town-name.example.com/history/1

我在nginx中通配了子域名,现在我用Google搜索并阅读了

http://codeigniter.com/user_guide/libraries/uri.html

http://codeigniter.com/user_guide/general/urls.html

http://codeigniter.com/user_guide/helpers/url_helper.html

但没有任何相关性。我需要这样做,如果将另一个城镇添加到数据库中,它将解析新城镇及其细节。

我看到很多关于重写,重定向等的讨论,但我特别需要使用子域名城镇名作为类变量。如果可能的话,在一个创意世界中,我可以同时使用它们,但我怀疑这是可能的吗?

我已经在普通的旧PHP中使用了好几年了。现在我想升级到codeigniter而不破坏我的旧结构(如果可能的话)(还有一个很好的理由)。

谢谢你。

1 个答案:

答案 0 :(得分:1)

你可以做到。我正在为我的一个项目做这件事。

在控制器的构造函数中,只需explode当前网址并获取子网域并将其作为变量传递给您的方法

public class Controller extends CI_Controller {

    $subdomain = null;

    public __construct() 
    {
        // explode url here and set $this->subdomain = the actual subdomain
    }

    public function index() 
    {
        if($this->subdomain == null) {
            show_404();
        } else {
            // do what you wish
        }
    }
}