0

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

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 中对子域进行了通配符,现在我已经用谷歌搜索并阅读了

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(另外还有一个很好的理由)。

谢谢。

4

1 回答 1

1

你能行的。我正在为我的一个项目做这件事。

在控制器的构造函数中,只需分解当前 url 并获取子域并将其作为变量传递到您的方法中

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
        }
    }
}
于 2012-02-29T15:39:04.393 回答