Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

php - mvc specific layout with views

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...