0
I am creating a PHP framework and in my Controller class, I have a method render
, where I receive two parameters, the filename (which would be the view), and an array of variables that will be passed to that view.
function render($arquivo, $variaveis = [])
{
extract($variaveis); // extrai as variáveis que serão passadas para a view
require $arquivo;
}
These variables will be passed to the view this way:
$variavel1 = 'teste1';
$variavel2 = 'teste2';
$this->render('minha-view.php', compact('variavel1', 'variavel2'));
The problem
If I have a variable $arquivo
in my controller, and the value of that variable is any string, the value that is passed to my view would be the first parameter that was passed to the method render
. Example:
meucontrolador.php
$arquivo = 'arquivo.xyz.txt';
// passa a variável $arquivo para a view minha-view.php
$this->render('minha-view.php', compact('arquivo'));
my-view.php
<body>
<?php echo $arquivo; ?> <!-- ao invés de imprimir 'arquivo.xyz.txt' ele imprime 'minha-view.php' -->
</body>
Thanks in advance.