How to pick up an Array on all view pages

Asked

Viewed 281 times

0

I have a view that’s called by a master page, the problem is that step one 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)
  • I didn’t quite understand the question, the test you pass as a key of an array, after you try to give an echo. It comes as a key to the array, you give an Extract at some point in the received content ($data), if not, where the $test variable comes from?

  • Are you using any framework? Which one? I think it’s logical to send the $test variable back to the new view. Ex.: $this->view($content, Compact('test'));

  • I am creating a mini framework -> here the repository https://github.com/MMS2/dmvc , and yes I am extracting in the core/controller.php

  • Can you put an excerpt from the view method? Because everything is very vague. What you want is just to pass an array to a method and make the array data available to the other methods?

  • Redeclarando the array it picks up, but I would like to add automatic type, just call the content there it comes together. so there’s no way right?

  • public Function view($view, $data = array()){ $Vd = 'app/view/'. $view. '. php'; if(file_exists($Vd)){ $Extract = Extract($data); require_once $Vd; Return $Extract; }Else{ echo "there is no file ". $view." in <b>". $Vd." </b>"; } }

Show 1 more comment

1 answer

0


I believe one of the possible implementations (following as a basis the documentation provided in the comment where the source is available on github) that if you turn $data into a class attribute (making it static), you could reuse the data, in the example below any added value will be stored in the $date class 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>";
        }

    }   
}
  • Sileno, Thank you. I had tried using Static method... but without the array_marge, it worked =D

  • Needing just post that community helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.