3

我想明确这一点。我正在使用代码点火器。

在构建 Web 应用程序时,在 codeigniter 中 OOP 设计的更好/正确/标准/最受赞赏的方式是什么/哪种?

一般来说,

一、创建一个像User这样的自定义类并具有登录功能?像,

     class User{ 
            var $db, $id, $username, $password, $role; //have all user attributes here

            function __construct() {
               //instance for accessing the database
               $this->db =& get_instance();
            }

            public function login(){
                //do the login here
                $this->db->load->model('user_model');   
                $success = $this->db->user_model->login($this->username, $this->password);      
                //isn't this redundant using models? or should I rename the model function name like checkLogin?
                return $success;
            }

            //other class functions 
        }

或者

二、直接从控制器调用模型中的登录函数而不创建用户对象?

o_o

在方案 1 中,我使模型仅在数据库中查询。我还可以让“用户”对象像“客户”、“管理员”一样被继承,并为每个对象添加特定功能。我的优势是让 CI 控制器专注于布局和将变量传递给视图,这主要是它的目的(不太确定),并让我的自定义类/对象处理我的 Web 应用程序中的操作。 但是我觉得我的登录功能有冗余,我在模型中调用了另一个登录功能。这个好吗?

在方案 2 中,让我的控制器处理所有操作(如登录、注册或创建日志并接收输入帖子并将变量传递给视图)是否合适?

我真的需要一些指导。我的 OO 技能需要提高。

非常感谢!

4

3 回答 3

2

首先,我认为阅读这篇文章对您很有用:

访问控制列表

我现在正在开发一个使用 CodeIgniter 的项目,我们在这里使用的方法可能也适合您。如果您需要检查授权,我认为最好的地方是在类的构造函数中,如果用户尚未注册,则调用登录函数。但这是一种有点通用的方法,所以如果你有一些函数应该只对注册用户可用的类,那么你应该在函数内部进行检查。

于 2012-06-14T06:42:26.700 回答
1

If you really want to follow strict OO model you should consider using DataMapper. Many will say it will slow down the application, true, but this is the price you pay for full OO. What you can do is create a User class that handles all the data manipulation and move the login to a Auth library, that would work with your User model. And remember Models work with/do data manipulation Controllers store the business logic.I am also learning and hope this post helps you.

于 2012-06-14T08:38:28.050 回答
0

如果这个模型将被使用更多次,那么创建一个对象并在构造函数中加载模块总是一个好主意。

但我不建议你 $this->db =& get_instance();在构造函数中使用。这应该已经自动加载了。

于 2012-06-14T06:35:54.647 回答