带有视图的mvc特定布局 [英] mvc specific layout with views

查看:97
本文介绍了带有视图的mvc特定布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图了解有关PHP中MVC的更多信息.但是我遇到了一个问题:我想在特定div内的特定位置显示我的视图.我怎样才能做到这一点?这就是我现在所拥有的:

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:

控制器:

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");
        }
    }
}

查看:

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>";
    }
}

我想在按钮下方显示找不到Funcionario" .我怎样才能做到这一点?我需要重定向页面吗?

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

P.S .:我没有使用任何框架.

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

推荐答案

您的代码存在一些问题,但首先-您的问题:请仔细阅读

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.

在旁边.无论是否显示未找到Funcionario"消息,您都会在运行脚本的那一刻知道.并且在渲染模板之前.无需重定向页面.

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. 模型不是数据访问对象.它是一个由多个类组成的层.这是关于此主题的更长的 rant .向下滚动到一些笔记"部分.

  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.

构造函数不应包含任何复杂的计算.也不应创建其他对象的实例,这些实例将在以后的实例中使用.它在您正在构造的对象和类的名称之间建立紧密的耦合(在您的情况下为LogarModel' and LogarView`).如果阅读时间太长,它会违反 SRP .. Elegantcode.com/wp-content/uploads/2008/12/srp1.jpg"rel =" nofollow noreferrer>这张图片将使其更加清晰.

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.

如果您在php中有一个emty构造函数,则可以将其删除(在您的View类中).

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

您的<script>标记为缺少属性.

编写代码= P

这篇关于带有视图的mvc特定布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆