codeigniter中的预控制器挂钩

时间:2012-10-18 13:50:08

标签: codeigniter hook

我在我的项目中使用预控制器钩子编码器

描述:   我们正在使用子域概念和三个模板(主题)。例如:我的网站是xyz.com。这是第一个模板。

使用此xyz网站进行一些业务注册。例如。美国广播公司(企业)。我们创建了abc.xyz.com。 abc选择2个模板。浏览器中的abc.xyz.com需要显示第二个模板。它没有显示第二个模板。它只显示第一个模板。

当我们多次点击网站上的任何链接时,模板2将设置为abc.xyz.com链接。

我正在使用codeigniter。加载会话,自动加载文件中的数据库。

我使用precontroller hook来检查url是xyz还是任何子域abc.xyz.com 如果网址是子域名,我正在设置模板。

但是当abc.xyz.com在浏览器中时,模板没有显示。当我刷新网址进行一些点击或点击任何一个标题链接时,它显示了业务abc的实际模板。

请帮我解决这个问题或为我提供一些解决方案。

<?php
class Subdomain_check extends CI_Controller{
public function __construct(){
    parent::__construct();
    $this->CI =& get_instance();

    if (!isset($this->CI->session))
    {
    $this->CI->load->library('session');
    }

}
function checking()
{

$subdomain_arr = explode('.', $_SERVER['HTTP_HOST']); //creates the various parts

if($subdomain_arr[0] == 'www')
{
    $subdomain_name = $subdomain_arr[1]; //2ND Part            
}
else
{
    $subdomain_name = $subdomain_arr[0]; // FIRST Part                      
}        

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache"); 

if( $subdomain_name != 'xyz' )
{
    $where = array();
    $where['subdomain_name'] = $subdomain_name;
    $where['status'] = 1;
    $this->db->from('subdomain_map');
    $this->db->where($where); 
    $query = $this->db->get();         
    if($query->num_rows() < 1)
    {
    header('Location:http://xyz.com/index.php?/error');
    }   
    else
    {
    $result = $query->row_array();
    $this->CI->session->set_userdata('subdomain_id',$result['subdomain_id']);
    $this->CI->session->set_userdata('subdomain_name',$result['subdomain_name']);
    $org_id = gat_organisationid_using_subdomainid($result['subdomain_id']);
    $this->CI->session->set_userdata('organisation_id', $org_id);

    if($org_id)
    {
    $templ_id = get_templid_using_organisationid($org_id);
    $org_logo = get_organisation_logo($org_id);
    }  

    if($templ_id){
    if($this->session->userdata('pinlogin'))
    $this->CI->session->set_userdata('template_set', 4);
    else
    $this->CI->session->set_userdata('template_set', $templ_id);               
    }

    if($org_logo)
    $this->CI->session->set_userdata('org_logo', $org_logo);              
    }           
}
else
{
    $this->CI->session->unset_userdata('subdomain_id');
    $this->CI->session->unset_userdata('subdomain_name');
    if( $this->CI->session->userdata('user_id') && $this->CI->session->userdata('user_category')<=2 )
    {
    $this->CI->session->unset_userdata('organisation_id');
    $this->CI->session->unset_userdata('org_logo');
    }
}    
}

}

1 个答案:

答案 0 :(得分:0)

以下是支持每个子域名自定义主题所需的基本检查

// Gets the current subdomain
$url = 'http://' . $_SERVER['HTTP_HOST'];
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);

// store $host[0], which will contain subdomain or sitename if no subdomain exists
$subdomain = $host[0];   

// check for subdomain
if ($subdomain !== 'localhost' OR $subdomain !== 'mysite')
{
    // there is a subdomain, lets check that its valid
    // simplified get_where using activerecord
    $query = $this->db->get_where('subdomain_map', array('subdomain_name' => $subdomain, 'status' => 1));

    // num_rows will return 1 if there was a valid subdomain selected
    $valid = $query->num_rows() === 1 ? true : false;

    if($valid)
    {
         // set theme, user_data, etc. for subdomain.
    }
    else
    {
        // get user out of here with redirect
        header('Location: http://' . $_SERVER['HTTP_HOST'] . '/error');
        exit();
    }
}

请注意,在使用带有codeigniter的子域名时,您应该设置配置&gt; base_url到以下内容:

$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/poasty/poasty-starterkit/';

这将确保site_url()和其他CI助手之类的东西仍能正常工作。

通过阅读您的代码,我建议您使用更多Codeigniters内​​置功能,例如您的__construct函数有很多不必要的代码:

原始代码

public function __construct(){
    parent::__construct();

    /**
    * CI already exists 
    * since this controller extends CI_controller, there is already and instance of CI available as $this.
    $this->CI =& get_instance();
    */

    /**
    * duplicate check, CI checks if library is loaded
    * and will ignore if loaded already
    if (!isset($this->CI->session))
    {
        $this->CI->load->library('session');
    }
    */

    $this->CI->load->library('session');

}

针对Codeigniter进行了优化

public function __construct()
{
    parent::__construct();
    $this->CI->load->library('session');
}

我建议您阅读Codeigniter user_guide,以便更好地了解codeigniter可以执行的操作。 @see http://codeigniter.com/user_guide/

我希望你觉得这很有帮助!