How to get an Array on all view pages

0

I have a view that is called by a master page , the problem is that I pass a array and the second page called, but the data does not arrive as expected.

ex.

//controller index
class Index{

public function index(){
$data = array(
'conteudo'=>'index/index', 'teste'=>'teste'

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

}

//Página view template

$this->view($conteudo);

//segunda view "index/index"

echo $teste <<<< (esta variavel retorna vazia)
    
asked by anonymous 11.07.2014 / 14:58

1 answer

0

I believe one of the possible implementations ( based on the documentation informed in the comment where the source is available in github ) that if you transform the $ data into an attribute of the class (making it static), you could reuse the data, in the example below all the added value will be stored in the class's $ data attribute, if the field already exists it overwrites, if it does not exist it creates.

<?php

class Controller{
    public $model;
    public $view;
    public static $data = array(); // Alterado

    public function model($model){

        $md = 'app/model/'.$model.'.php';

        if(file_exists($md)){
            require_once $md;   
            return new $model();    
        }else{

            echo "nao existe o arquivo   ".$model." em <b>".$md."</b>";
        }


    }   

    public function view($view, $data = array()){
        $vd = 'app/view/'.$view.'.php';

        if(file_exists($vd)){
            self::$data = array_merge(self::$data, $data); // Alterado
            $extract = extract(self::$data); // Alterado
            require_once $vd;   
            return $extract;    
        }else{

            echo "nao existe o arquivo ".$view." em <b>".$vd."</b>";
        }

    }   
}
    
11.07.2014 / 15:36