2012-05-14 2 views
18

컨트롤러에서 글로벌 변수는CodeIgniter는 : 나는 CodeIgniter의에 아주 안돼서, 나는 내가 갈 동안, 절차 코딩, 쉽게 고칠 수 있었다 문제에 실행

현재의 문제는 : 내가 가진 내가하지 않은 있도록

$data['title'] = 'Page Title'; 
$data['robots'] = 'noindex,nofollow'; 
$data['css'] = $this->config->item('css'); 

컨트롤러에 그들이 "글로벌"할 수있는 방법이 아닌가 : 당신이 볼 수 있듯이이 컨트롤러

class Basic extends Controller { 

    function index(){ 
     $data['title'] = 'Page Title'; 
     $data['robots'] = 'noindex,nofollow'; 
     $data['css'] = $this->config->item('css'); 
     $data['my_data'] = 'Some chunk of text'; 
     $this->load->view('basic_view', $data); 
    } 

    function form(){ 
     $data['title'] = 'Page Title'; 
     $data['robots'] = 'noindex,nofollow'; 
     $data['css'] = $this->config->item('css'); 
     $data['my_other_data'] = 'Another chunk of text'; 
     $this->load->view('form_view', $data); 
    } 
} 

, 일부 배열 항목은 반복 반복 eac에 입력하려면 h 기능? 같은 뭔가 (그러나 이것은 나에게 오류를 제공) : 사전에

class Basic extends Controller { 

    // "global" items in the $data array 
    $data['title'] = 'Page Title'; 
    $data['robots'] = 'noindex,nofollow'; 
    $data['css'] = $this->config->item('css'); 

    function index(){ 
     $data['my_data'] = 'Some chunk of text'; 
     $this->load->view('basic_view', $data); 
    } 

    function form(){ 
     $data['my_other_data'] = 'Another chunk of text'; 
     $this->load->view('form_view', $data); 
    } 

} 

Thnaks!

답변

28

당신이 할 수있는 것은 컨트롤러의 모든 방법에서 액세스 할 수 있습니다 "클래스 변수"를 만들 것입니다 그것을에 참조 할 수 있습니다. 생성자에서 이러한 값을 설정합니다.

class Basic extends Controller { 
    // "global" items 
    var $data; 

    function __construct(){ 
     parent::__construct(); // needed when adding a constructor to a controller 
     $this->data = array(
      'title' => 'Page Title', 
      'robots' => 'noindex,nofollow', 
      'css' => $this->config->item('css') 
     ); 
     // $this->data can be accessed from anywhere in the controller. 
    }  

    function index(){ 
     $data = $this->data; 
     $data['my_data'] = 'Some chunk of text'; 
     $this->load->view('basic_view', $data); 
    } 

    function form(){ 
     $data = $this->data; 
     $data['my_other_data'] = 'Another chunk of text'; 
     $this->load->view('form_view', $data); 
    } 

} 
+1

@Dalen : 오타를 수정 해 주셔서 감사합니다 .-) –

+0

당신을 환영합니다! – Dalen

+0

감사합니다! 한편, "$ this-> load-> vars ($ array)"가 제 예제에 정말 적합하다는 것을 알았 기 때문에 질문을 잊어 버렸습니다. 어쨌든 제공된 배열은 배열과 배열 사이에 배열을 전달해야합니다. 클래스 메소드 – Ivan

15

data라는 클래스 속성을 설정 한 다음 기본값을 Basic의 새 인스턴스를 만들 때 실행되는 첫 번째 컨스트럭터로 설정할 수 있습니다. 그런 다음 당신은 $this 키워드

class Basic extends Controller 
{ 
    var $data = array(); 

    public function __construct() 
    { 
     parent::__construct(); 
     // load config file if not autoloaded 
     $this->data['title'] = 'Page Title'; 
     $this->data['robots'] = 'noindex,nofollow'; 
     $this->data['css'] = $this->config->item('css'); 
    } 

    function index() 
    { 
     $this->data['my_data'] = 'Some chunk of text'; 
     $this->load->view('basic_view', $this->data); 
    } 

    function form() 
    { 
     $this->data['my_data'] = 'Another chunk of text'; 
     $this->load->view('form_view', $this->data); 
    } 
} 
+1

이 기능을 사용하려면 생성자에'parent :: __ construct();'를 추가해야합니다. –

+2

맞고 아마도 아직 자동로드되지 않은 경우 설정 파일을로드하십시오 – Dalen

+0

예! 구성하면 문제가 해결됩니다. – rechie

3

헤이 덕분에 여기 전역 변수가 사용자 왜 도우미를 볼 수

/* Location: ./application/core/MY_Controller */ 

class MY_Controller extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->data = array(
      'sidebar' => $this->load->view('sidebar', '' , TRUE), 
     ); 
    } 

} 

/* Location: ./application/controllers/start.php */ 
class Start extends MY_Controller { 

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

    public function index() 
    { 
     $data = $this->data; 

     $this->load->view('header'); 
     $this->load->view('start', $data); 
     $this->load->view('footer'); 
    } 
} 
+0

저에게 효과적입니다. –

0

을 잡고 내 snipet 입니까?

파일 :

/application/helpers/meta_helper.php 

내용 :

컨트롤러에서
<?php 
function meta_data() { 
return array("title" => null, "robots" => "noindex, nofollow"); 
} 

:

class Basic extends Controller { 

    function __construct(){ 
     parent::__construct(); 
     $this->load->helper('meta'); 
    }  

    function index(){ 
     $data['meta'] = meta_data(); //associate the array on it's own key; 

     //if you want to assign specific value 
     $data['meta']['title'] = 'My Specific Page Title'; 

     //all other values will be assigned from the helper automatically 

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

그리고보기 템플릿 :

<title><?php $meta['title']; ?></title> 

윌 출력 :

<title>My Specific Page Title</title> 

희망 의미 :-)하게!

0

너무 오래 동안. 다른 사람에게 도움이 될 수 있습니다. $ this-> load-> vars ($ data); 모든 뷰에서 $ 데이터 배열을 사용할 수 있도록 MY_controller 코어에 추가합니다.

/* Location: ./application/core/MY_Controller */ 

class MY_Controller extends CI_Controller { 

function __construct() 
{ 
    parent::__construct(); 
    $data['title'] = 'Page Title'; 
    $data['robots'] = 'noindex,nofollow'; 
    $data['css'] = $this->config->item('css'); 
    $this->load->vars($data); 
} 

} 

/* Location: ./application/controllers/start.php */ 
class Start extends MY_Controller { 

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

public function index() 
{ 
    $data['myvar'] = "mystring"; 

    $this->load->view('header'); 
    $this->load->view('start', $data); 
    $this->load->view('footer'); 
} 
} 
관련 문제