-3

I'm trying to learn more about MVC in PHP. But I have encountered a problem: I want to show my view in a specific place within a specific div. How can I do this? This is what I have right now:

Controller :

class LogarController extends Controller {

    private $view;
    private $modelDAO;

    function __construct() {
        parent::__construct();
        $this->modelDAO = new LogarModel();
        $this->view = new LogarView();
    }

    /**
     * metodo Login().
     * funcao para logar o funcionario ja cadastrado ao banco de dados.
     */
    public function Login() {
        if ($this->modelDAO->Login($_POST['funcionario'], $_POST['senha'])) {
            $idFuncionario = $this->modelDAO->rows['idFuncionario'];
            $this->sessao = new Session();
            $this->sessao->set_value("logado",true);
            $this->sessao->set_value("idFuncionario",$idFuncionario);
            $this->redirect("view/funcionario/index.php");
        } else {
            $this->view->show("Funcionario not found");
        }
    }
}

View:

class view {

    function __construct(){}

    function __set($var,$value){
        $this->var = $value;
    }

    function __get($var){
        return $this->var;
    }

    function render($render){
        require "view/template/" . $render . ".php";
    }
    function show($value){
        $this->value = $value;
    }
    function alertar($value){
        echo "<script>alert('{$value}')</script>";
    }
}

I want to show "Funcionario not found" under the button. How can I do this? Do I need to redirect the page?

P.S.: I'm not using any framework.

ElefantPhace
  • 3,806
  • 3
  • 20
  • 36

2 Answers2

1

There are several problems with that code of yours, but first - your problem: read carefully this article. It will explain how to utilize PHP's templating capabilities.

Beside. Whether to show or not the "Funcionario not found" message, you will know at the moment you run the script. And before rendering the template. There is no need to redirect the page.

Now that you original problem has a solution, the rest of the mess:

  1. Model is not a data access object. It is a layer, composed from a multitude of classes. Here is a longer rant on the subject. Scroll down to "some notes" section.

  2. Constructors should not contain any complicated computation. Nor should it be creating instances of other objects which will be later used in the instance. It creates tight coupling between the object you are construction and names of the classes (in your case, LogarModel' andLogarView`). It violates SRP .. if too long to read, this picture will make it clearer.

  3. If you have an emty constructor in php, then you can just remove it ( in your View class).

  4. Your <script> tag is missing attributes.

  5. And whats you should stick to single language, when writing code =P

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
0

Basically your view class handles the output by old-fashioned echoing of the variables your model and controller have created, with HTML presentation. There are lots of other ways to do it, but this would be one simple way.

 class LogarView {

      public function show($message){
           echo '<input type="button" value="Click Me"/><br/>';
           echo $message;
      }

 }

The above would output an HTML button and "Funcionario not found" underneath.

fred2
  • 1,015
  • 2
  • 9
  • 29
  • im doing it, but the page does not refresh for the __get return the message ($this->value) – user1258260 Mar 20 '12 at 01:41
  • Ok, I see what you have. The output is done in the "view/template/" . $render . ".php"; template. That is the file that will need to be edited so it knows how to output your variable "Funcionario not found" string in the right place. There's no simple answer that can be given without knowing how that file operates. – fred2 Mar 20 '12 at 01:51
  • this file is only php file with form and and in the end a value ?> to show the "funcionario not found" – user1258260 Mar 20 '12 at 01:57
  • Then your PHP looks correct, and should already be outputting your message. Do you get any error messages? – fred2 Mar 20 '12 at 02:02
  • it is showing only when i render the page and no, i dont get errors. – user1258260 Mar 20 '12 at 02:08
  • I don't really understand the problem. "It is showing only when I render the page" sounds like the correct behaviour. – fred2 Mar 20 '12 at 03:53