2012-05-14 7 views
18

Jestem bardzo newbie na CodeIgniter, a gdy idę na I napotkasz problemy, że w kodowaniu procesowego, były łatwe do naprawieniaCodeIgniter: zmienne globalne w kontrolerze

Obecny problem jest: Mam kontroler

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); 
    } 
} 

Jak widać, niektóre elementy tablicy powtarzać w kółko:

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

nie jest sposobem, aby uczynić je „globalne” w sterowniku, tak że nie mam wpisać je dla eac funkcja h? Coś podobnego (ale to daje mi błąd):

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 wyprzedzeniem!

Odpowiedz

28

Co można zrobić, to „zmienne klasy”, które można uzyskać z dowolnego sposobu w kontrolerze. W konstruktorze ustawiasz te wartości.

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: Dzięki za naprawienie tej literówki :-) –

+0

nie ma za co! – Dalen

+0

Dzięki! W międzyczasie zapomniałem o tym, ponieważ stwierdziłem, że "$ this-> load-> vars ($ array)" pasuje naprawdę fajnie do moich przykładów ... W każdym razie dostarczone rozwiązanie jest jeszcze ładniejsze, jeśli muszę przekazać tablicę pomiędzy metody klasy – Ivan

15

Można ustawić właściwość klasy o nazwie dane, a następnie ustawić jej domyślne wartości na contructor, co jest pierwszą czynnością wykonywaną po utworzeniu nowej instancji na Basic. Następnie można odwoływać się do niego z $this hasła

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

Musisz dodać 'parent :: __ construct();' do konstruktora, aby to działało. –

+2

Dobrze, i prawdopodobnie również załadować plik konfiguracyjny, jeśli jeszcze nie został automatycznie załadowany – Dalen

+0

tak! konstrukcja rozwiąże problem. – rechie

3

hej dzięki oto moja snipet jest to zmienna globalna trzyma widok

/* 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

Dzięki za pracę dla mnie –

0

Dlaczego nie użytkownik pomocnika?

Plik:

/application/helpers/meta_helper.php 

Treść:

<?php 
function meta_data() { 
return array("title" => null, "robots" => "noindex, nofollow"); 
} 

W kontrolerze:

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); 
    } 

I w widoku Szablon:

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

wyświetli:

<title>My Specific Page Title</title> 

Nadzieja, że ​​sens :-)!

0

Mimo że była tak długa. Może być pomocne dla innych, możesz użyć $ this-> load-> vars ($ data); w rdzeniu MY_controller, aby udostępnić tablicę danych $ we wszystkich widokach.

/* 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'); 
} 
}