Views with php transition with the controller

1

I'm new to php, so I've got a lot of it. Even because I have used sublime text with apache and php installed separately (use Ubuntu with OS). Well my question regarding views and php is:
I'm not understanding how to transition the controller to the view. If I should mix php code with html tags, or if it has a way to include in the html file what is passed by a php code.

I know that my question is very general and initial, but if someone has a didactic example of how I do it, or if someone knows of some example project so I see and understand it also helps. What else I have found on the net is concept and this I have already understood, now apply this in practice is another 500

    
asked by anonymous 17.07.2015 / 01:33

2 answers

2

Luan,

Are you studying MVC without any right market framework? To be able to understand how it works and improve its way of programming, when studying with PHP I made a project of a phonebook using MVC without framework follows link in my github: link

Example of the code below:

<?php
/*
 * Essa classe é responsável por renderizar os arquivos HTML
 * 
 * @package Exemplo simples com MVC
 * @author João Manoel
 * @version 0.1.1
 * 
 * Diretório Pai - lib
 * Arquivo - View.php 
 */
 class View
 {
  /**
  * Armazena o conteúdo HTML
  * @var string
  */
private $st_contents;

/**
* Armazena o nome do arquivo de visualização
* @var string
*/
private $st_view;

 /**
 * Armazena os dados que devem ser mostrados ao reenderizar o 
 * arquivo de visualização
 * @var Array
 */
 private $v_params;

 /**
 * É possivel efetuar a parametrização do objeto ao instanciar o mesmo,
 * $st_view é o nome do arquivo de visualização a ser usado e 
 * $v_params são os dados que devem ser utilizados pela camada de    visualização
* 
* @param string $st_view
* @param Array $v_params
*/
function __construct($st_view = null, $v_params = null) 
{
    if($st_view != null)
        $this->setView($st_view);
    $this->v_params = $v_params;
}   

/**
* Define qual arquivo html deve ser renderizado
* @param string $st_view
* @throws Exception
*/
public function setView($st_view)
{
    if(file_exists($st_view))
        $this->st_view = $st_view;
    else
        throw new Exception("View File '$st_view' don't exists");       
}

/**
* Retorna o nome do arquivo que deve ser renderizado
* @return string 
*/
public function getView()
{
    return $this->st_view;
}

/**
* Define os dados que devem ser repassados à view
* @param Array $v_params
*/
public function setParams(Array $v_params)
{
    $this->v_params = $v_params; 
}

/**
* Retorna os dados que foram ser repassados ao arquivo de visualização
* @return Array
*/
public function getParams()
{
    return $this->v_params;
}

/**
* Retorna uma string contendo todo 
* o conteudo do arquivo de visualização
* 
* @return string
*/
public function getContents()
{
    ob_start();
    if(isset($this->st_view))
        require_once $this->st_view;
    $this->st_contents = ob_get_contents();
    ob_end_clean();
    return $this->st_contents;   
}

/**
* Imprime o arquivo de visualização 
*/
public function showContents()
{
    echo $this->getContents();
    exit;
}
}
 ?>
    
17.07.2015 / 04:27
0

This is not a good practice to mix controller (system logic) with view (visual part of the program that interfaces with the user), to avoid the problems caused by this mix and gain more operational security (Web Designer and Developer can work at the same time without interfering with each other) is used templates .

Sample templates:

Smarty: Download and Documentation Link

Dwoo: Download link and documentation

Example usage (more examples are provided in the documentation for other features):

<html>
  <head></head>
  <body>
    <blockquote>

    Palavra 1 <br/>
    Palavra 2 <br/>
    {$nome}      <br/>
    {$sobrenome}
    </blockquote>

  </body>
</html>

<?php
// auto-loader
include 'dwooAutoload.php';

// cria objeto Dwoo 
$dwoo = new Dwoo();

// le o templete acima definido
$tpl = new Dwoo_Template_File('tmpl/knock.tpl');

// valores dinamicos que serão injetados no template
$data = array();
$data['nome'] = 'Bill';
$data['sobrenome'] = 'Gates';

// injeta os valores e exibe a pagina, note que o nome do indices são identicos as 
//variaveis dentro do template, o dwoo irá fazer a injeção paseado nos nomes identicos.
$dwoo->output($tpl, $data);
?>
    
17.07.2015 / 01:42