2014-05-20 2 views
1

이것은 내 컨트롤러입니다. 위의 세 줄을 전역 변수로 사용해야합니다. 그래서 모든 기능이 그것을 사용할 수 있습니다. 내가 어떻게 할까?전역 변수 세션 데이터를 선언하는 방법

class Some_Controller extends CI_Controller { 

    $this->load->library('session'); //Must be global      
    $this->session->set_userdata('email', '[email protected]'); //Must be global 
    $data['current_user']=$this->session->userdata('email'); //Must be global 


    public function index(){ 

    $data['people'] = $this->some_model->getPeople(); 
    $data['mapList'] = $this->some_model->getMapped(); 
    $data['appServList'] = $this->some_model->getApp_serv(); 


    $this->load->view('templates/header.php',$data); 
    $this->load->view('some_page/index.php',$data); 
    /*Serves as the homepage. Shows the list for services, mapped services to an application and the list for application, 
    from here you can easily add edit and hide items*/ 

} 
+1

일단 세션을 설정하십시오. 어디서나 전화를 걸 수 있습니다. 전체적으로 설정할 필요가 없습니다 ...! –

+0

하지만 작동하지 않습니다 – user3655316

답변

1

세션을 자동으로 시작하려면 $autoload['libraries'] = array('session');을 autoload.php에서 설정할 수 있습니다. session에 데이터를 설정하면 원하는 곳 어디에서나 사용할 수 있습니다. 를 사용하면 현재 위치에서 컨트롤러에 그 function.after 로그인에 그것을 할 수있는 세계적으로 그것을 할 필요가 없습니다 세션에서 데이터를 설정 당신이 원하는 $this->current_emai;public $current_email=$this->session->userdata('email');

및 액세스 current_email을 넣어합니다.

$objCI =& get_instance(); //now CI object can be used 
echo $objCI->current_email; 
+0

아래 두 줄은 어떻게해야합니까? – user3655316

+0

지금보십시오. 도움이 되나요? –

+0

그것은 $ this가 허용되지 않는다고 말한다. 나는 또한 전망에있는 전자 우편을 출력 할 필요가있다. 그 값을 인식하지 못합니다. 세션 데이터를 어떻게 입력합니까? – user3655316

0

그냥이 클래스의 모든 기능을 사용할 수 있습니다 gloabal varibale를 추가 공개

class Some_Controller extends CI_Controller { 

//로 그 변수를 선언 할 수있는 뷰에 변수에 액세스 할 수 있습니다.

첫째와 무관 한, 당신이 아닌 일반의 상단에 매 시간 만 성공적인 인증 절차 후 같은 세션 데이터를 설정해야 :

public $global_variable = "global_example"; 

    $this->load->library('session'); //Must be global  $this->session->set_userdata('email', '[email protected]'); //Must be global 
    $data['current_user']=$this->session->userdata('email'); //Must be global 


    public function index(){ 




    $data['people'] = $this->some_model->getPeople(); 
    $data['mapList'] = $this->some_model->getMapped(); 
    $data['appServList'] = $this->some_model->getApp_serv(); 


    $this->load->view('templates/header.php',$data); 
    $this->load->view('some_page/index.php',$data); 
    /*Serves as the homepage. Shows the list for services, mapped services to an application and the list for application, 
    from here you can easily add edit and hide items*/ 

} 
-1

여기에서 주목할 가치가 두 지점이 있습니다 제어 장치.

두 번째와 중요한 것은 개의 전역 변수으로 이해합니다. 앱 디자인을 시작할 때 종류의 개의 컨트롤러가 필요하다고 느낍니다. 예를 들어, Admin_Controller, Backoffice_Controller 또는 Ajax_Controller 컨트롤러가있을 수 있습니다. 일부는 다른 사람을 확장시킬 수 있습니다. 그래서 전역 변수라고 말하면 그런 메커니즘이 필요하다고 생각합니다. 따라서 상위 컨트롤러에서 변수를 설정하면 응용 프로그램의 모든 파생 컨트롤러에서 변수를 사용할 수 있습니다. 다른 모든 것에 대한 일반 로직/데이터를 포함하는 모든 응용 프로그램 컨트롤러에 대한 부모 컨트롤러로 Base_Controller을 만드는 것이 좋습니다.

그러나 요점은 CodeIgniter가 컨트롤러 상속을 기본적으로 지원하지 않는다는 것입니다. CI Base Controllers 프로젝트를보고 문서를 읽으십시오. 그것은 당신이 필요로하는 것을 달성하는 데 도움이되며, 더 나은 DRY 지향 응용 프로그램 설계를 제공합니다.

관련 문제