Zend Framework 用户身份验证(MVC 问题)
我在理解 MVC 概念时遇到了一些困难。
我正在构建一个用户模型,你知道吗?应用程序_模型_用户。他们说模型应该只包含结构......而业务逻辑应该放在控制器中。
因此,考虑一个名为authenticate($user, $password)的函数。如果输入的用户名和密码有效,该函数将返回 true,否则返回 false。我应该把这个功能放在哪里?在控制器身份验证中还是在模型用户中?
谢谢你!
I'm getting some trouble understanding the MVC concepts.
I'm building a User model, you know? Application_Model_Users. They say that the models should only contain the structure... and the business logic should be put in the controller.
So, consider a function called authenticate($user, $password). This function will return true if the username and password entered is valid or false otherwise. Where should I put this function? In the controller Authentication or in the model Users?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与模型相关,每当您需要检索数据(从数据库、Web 服务、文件系统)或保存数据时,您都需要一个模型来完成这项工作。在 MVC 中,模型并不被理解为映射表,可能更像是映射器。 Zend 在他们的网站上有一些关于这方面的信息,它可以帮助您更多地了解 mvc。
当涉及到用户身份验证时,您当然应该在用户模型中实现身份验证功能,我认为您将针对表或类似内容进行数据库检查。
以防万一您还没有使用它,Zend 附带了一个用于身份验证的包:Zend_Auth (http://framework.zend.com/manual/en/zend.auth.html),它可以加快在您的系统中实现安全性的速度。应用。
Related to the Model, whenever you need to retrieve data(from DB, Web service, filesystem) or save data, you need a model to do the job. In MVC, a model is not understood as a mapped table, maybe more like a mapper. Zend has some info about this at their site, it could help you understanding mvc a bit more.
When it comes to user authentication, you should certainly implement the authenticate function inside the Users model, I would think you will do a database check against a table or similar.
Just in case you are not already using it, Zend comes with a package for auhtentication: Zend_Auth (http://framework.zend.com/manual/en/zend.auth.html) , it could speed up implementing the security at your application.
虽然模型操作通常包括存储操作(DB、服务程序等),但并不限于此。据我所知,模型应该包含业务逻辑实体,即代表业务实体的类,例如 User、Person、Customer 等。每个类应该定义自己的操作方法,例如,Person 模型类应该允许你获取一个人的名字,根据他/她的出生日期计算他/她的年龄等。
此外,应该有专门的类用于模型存储和检索。使用这些类,您可以使用某些条件等获取所有客户,或仅获取一个客户,或者保存修改后的客户类实例(例如,客户更改了他/她的地址或电话号码)。
这将存储/检索操作与业务登录操作分开。
因此,根据您的问题,您的模型可能有一个类,允许您通过用户名和密码查找一个用户。如果找到用户,您可以返回 Model_User 类实例(在示例中)。然后,使用标准的 Zend_Auth 类,或者扩展它来创建您自己的身份验证类,您可以使用一些登录表单参数来执行用户身份验证。
遵循 Zend Framework 快速入门指南,有有关 Zend Framework 中 MVC 的基础知识。此外,您还可以找到一些有关 Zend_Db 和相关类的资源,以允许数据库交互。还有 Zend_Db_Table、Zend_Db_Table_Rowset 和 Zend_Db_Table_Row 类,您可以扩展它们以满足您的模型存储需求。
我有一个个人解决方案,我为我的(示例)Model_UserTable 类扩展 Zend_Db_Table,用于存储或查询我的 Model_User 实体。我的 Model_User 类扩展了 Zend_Db_Table_Row。
Although Model operations often include storage operations (DB, servicer, etc), it is not limited to that. Model, as far as I know, should countain Business logic entities, this is, classes that represent your business entities, like User, Person, Customer, etc. Each class should define its own operation methods, in example, a Person model class should allow you to get a person's name, calculate his/her age according to his/her birth date, etc.
Also, there should be specialized classes for Model storage and retrieval. With these classes you could fetch all your Customers, or only one, using certain conditions, etc, or save a modified Customer class instance (in example, a customer changed his/her address or phone number).
This separates the storage/retrieval operations from Business login operations.
So, according to your question, your model could have a class that allows you to find one user by its user name and password. If the user is found, you could return a Model_User class instance (in example). Then, using the standard Zend_Auth class, or extending it to create your own authentication class, you can use some Login form parameters to perform the user authentication.
Follow the Zend Framework quick start guide, there are the basics about MVC in Zend Framework. Also, there you will find some resources about Zend_Db and related classes, to allow DB interaction. There are also Zend_Db_Table, Zend_Db_Table_Rowset and Zend_Db_Table_Row classes, that you could extend to fit your model storage needs.
I have a personal solution where I extend Zend_Db_Table for my (in example) Model_UserTable class, used to store or query my Model_User entities. And my Model_User class extends Zend_Db_Table_Row.