2014-11-01 3 views
0

Codeigniter를 동적 하위 도메인과 함께 사용하고 있지만 컨트롤러의 각 메소드에서 동적 하위 도메인의 계정을 가져와야합니다. 내가 좋아하는 모든 방법 도메인을 얻고 그것없이 $ 데이터에 추가 할 수있는 방법을 찾고 있어요 :Codeigniter 하위 도메인 와일드 카드가 모든 방법으로 계정 가져 오기

<?php 

class Dashboard extends CI_Controller { 

    function index() 
    { 
     $subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2); 

     $subdomain_name = $subdomain_arr[0]; 

     $this->db->from('accounts')->where('subdomain', $subdomain_name); 

     $query = $this->db->get(); 

     $account = $query->row(); 

     $data['account_id'] = $account->id; 

     $data['account_name'] = $account->name; 

     $this->load->view('index', $data); 
    } 

    function clients() 
    { 
     $subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2); 

     $subdomain_name = $subdomain_arr[0]; 

     $this->db->from('accounts')->where('subdomain', $subdomain_name); 

     $query = $this->db->get(); 

     $account = $query->row(); 

     $data['account_id'] = $account->id; 

     $data['account_name'] = $account->name; 

     $this->load->view('clients', $data); 
    } 
} 

답변

2

Class Constructor 내에서 한 번 수행을 한 다음 다른 모든에서 같은 변수에 액세스 할 수 있습니다 행동 양식.

As per docs :.

"생성자가 일부 기본값를 설정하거나 클래스의 인스턴스가 기본 프로세스를 실행해야하는 경우 는 생성자가 값을 반환 할 수 유용하지만 그들이 할 수있는 일부 기본 작업을 수행하십시오. "

<?php 

class Dashboard extends CI_Controller { 

    public $data = array(); 

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

     $subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2); 
     $subdomain_name = $subdomain_arr[0]; 
     $this->db->from('accounts')->where('subdomain', $subdomain_name); 
     $query = $this->db->get(); 
     $account = $query->row(); 
     $this->data['account_id'] = $account->id; 
     $this->data['account_name'] = $account->name; 
    } 

    public function index() 
    { 
     $data = $this->data; // contains your values from the constructor above 

     $data['title'] = "My Index"; // also use your $data array as normal 

     $this->load->view('index', $data); 
    } 

    public function clients() 
    { 
     $data = $this->data; // contains your values from the constructor above 

     $this->load->view('clients', $data); 
    } 

} 

참고 : CodeIgniter의 기능 기본, 그것은 같은 선언 할 수있는 가장 좋은 방법은 "공공"의 할지라도. 참조 : Public functions vs Functions in CodeIgniter

+0

내 사용자 정의 컨트롤러를 만들고 내 컨트롤러에서 모두 확장하여 계정 데이터를 가져 오는 생성자 메서드를 숨길 수있는 방법이 있습니까? – cristianormoraes

+0

@cristianormoraes, 귀하의 의견 문법이나 말씨를 이해하지 못합니다. – Sparky

관련 문제