MVC关系和DRY [英] MVC Relationships and DRY

查看:96
本文介绍了MVC关系和DRY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建用户登录系统.

I am trying to create a user login system.

我有一个称为AccessControl的类/控制器,其中包含用于创建,删除和登录用户的所有功能.

I have a class/controller called AccessControl which includes all the functions for creating, deleting and logging a user in and out.

我还有一个名为Users的模型,其中具有所有数据库函数,这些函数是从AccessControl类调用的.

I also have a model called Users which has all the database functions in it which are called from the AccessControl class.

我的AccessControl类:

My AccessControl class:

include_once('../models/User.php') ;

class AccessControl {
private $_systemKey ;

public function __construct()
{
    $this->_systemKey = 'QA>8fg)@z#t#:E60mj&MzHsm-lUj&b-}R%~<y$|nAuF)C3!r%+rT"Q<r$o?{_XR' ;
}

public function createUser($email, $password, $level)
{           
    $user_salt = $this->randomString() ;

    $password = $user_salt . $password ;
    $password = $this->hashData($password) ;

    if(!is_int($level))
    {
        return false ;
    }

    //Create verification code
    $code = $this->randomString() ;

    //SQL...
    $created = User->insertNewUser($email, $password, $level) ;

    if($created != false){
        return true; 
    }

    return false ;
}
}

在我的模型中:

class User extends Core {

public function fetchSalt($username)
{
    $result = $this->db->prepare("SELECT saltword FROM users WHERE username = ?") ;
    $result->execute(array($username)) ;
    return $result->fetchColumn() ;
}

public function insertNewUser($email, $password, $level)
{
    //SQL Insert...
}

}

我不确定我是否以正确的方式进行操作,因为createUser和insertNewUser是否不相同?我该如何更改使其变干?

I am not sure if I am doing this the right way, because aren't createUser and insertNewUser identical? How can I change things to make it DRY?

Core只是我的PDO连接类,我的所有模型都将从该类扩展.无论如何,这是计划.

Core is just my PDO connection class, all my models will extend from it. That's the plan anyway.

abstract class Core {
protected $db ;

function __construct()
{
    try{
        $this->db = new PDO("mysql:host=localhost;dbname=database", "user", "pass") ;
        $this->db->exec('set names utf8') ;
                    $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        $this->db->setAttribute(PDO::ATTR_PERSISTENT, TRUE) ;
    }
    catch(PDOEXCEPTION $e)
    {
        echo 'Database error: <i style="color:#993300;">'.$e.'</i>' ;
        die() ;
    }
}

function __destruct()
{
    $this->db = NULL ;
}
}

推荐答案

通常,如果您希望在这种情况下将代码重复降至最低,我建议您不要让每个模型都处理数据库工作.

Generally if you want to keep the code duplication to a minimum in this scenario I would advice you to not have each model handle the database work.

相反,您可以为每个接受和返回模型的模型提供一个网关.通常,对于简单的检索而言,插入,删除和更新查询不会有太大差异.所有通用代码都可以位于所有其他网关从其扩展的AbstractGateway中.然后,特定的网关只处理模型的特定事项,例如定义要使用的表或返回的模型.

Instead you can have a gateway for each model that accepts and returns models. Usually for simple retrieval, inserting, removing and updating the queries will not differ much. All the common code can be in an AbstractGateway all the other Gateways extend from. The specific gateways just handle the model specific things then, like defining the table to use or the model to return.

您还可以从中获得一些额外的好处.您可以轻松地将数据库网关换成另一个存储网关.这样,您甚至可以测试代码,而不必担心数据库连接.

You also get some added benefits from this. You can easily swap out the database gateway for another storage gateway. That way you can even test your code without having to worry about the database connection.

至于创建和插入用户方法,我在那没看到问题.从MVC的角度来看,createUser方法是Controller的方法,对于您要执行的操作,它称为Model的方法.

As for the create and insert user methods I don't see a problem there. From an MVC standpoint the createUser method is a method of the Controller and for the action you are performing it calls a method of the Model.

这篇关于MVC关系和DRY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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