PHP MVC - 将用户变量存储在控制器或模型中?
在控制器或模型中存储变量的最佳实践是什么? 例如,当脚本执行时。它从会话中获取用户 ID,并获取用户类型:超级管理员、管理员、服务代表、销售代表。我们还检查用户 ID 属于哪个帐户,并获取该帐户的所有设置。
我的问题是在控制器或模型中的哪里存储这些值?
先感谢您。
What is the best practices as far as storing variables in the Controller or Model?
For instance when the script is executed. it grabs the user id from the session and gets what type of user it is, Super Admin, Admin, Service Rep, Sales Rep. We also check to see what account the user id belongs too, and grab all the setting for that account.
My questions is where do i store these values, in the controller or model?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 PHP 中,考虑真正的 MVC 模型有点奇怪,因为您的模型、视图和控制器可以访问 $_SESSION。
例如,如果您要让用户登录,您的模型将执行以下操作:
显然必须花费这段代码,但它应该正确显示概念。我还在模型中使用了静态函数,但您可以使模型成为一个对象。
根据您想要的方式,您可以每次通过模型从数据库中获取设置,也可以将它们存储在会话中。将内容存储在 $_SESSION 中将使您减少数据库调用。实际上,模型操作 $_SESSION 或数据库。如果您的模型特定于某些事物(您可以创建自己的用户模型),那么您可以实例化该对象并将您的信息存储在私有成员中。
控制器的重点是从模型中获取信息,然后相应地渲染页面。实际上,MVC 数据流是这样工作的:
(这是可选的,也许控制器不需要模型中的任何内容)
(如果您在上一步中提出了请求,则会发生这种情况)
In PHP, it is a little strange to think about a true MVC model, because your model, view, and controller can access the $_SESSION.
If, for example, you are going to log a user in, your model would do the following:
Obviously this code has to be expended upon, but it should display the concepts correctly. I also used static functions in the model, but you can make the model an object.
Depending on how you want to do it, you either fetch the settings every time form the database through the model or you can store them in the session. Storing things in the $_SESSION will allow you to have less database calls. In practice, the model manipulates the $_SESSION or the database. If your model is particular to something (you could make your own user model), then you instantiate that object and store your information in private members.
The point of the controller is to take information form the model and then render your page accordingly. Really a MVC dataflow works this way:
(this is optional, maybe the controller doesn't need anything from the model)
(Happens if you made a request form the previous step)
您将它们存储在模型中(从数据库中获取它们),使用控制器拉取它们(在页面加载时),然后在视图中显示它们的结果(通过在需要时调用控制器类)。
这就是MVC的基本理论...
祝你好运!
我会给你一个可以出售的汽车对象的简单例子...这个例子很糟糕,但是你可以从中了解 MVC 是如何工作的...
?>
You store them in the model (Grab them from DB), you pull them with the controller (On page load), and you show the result of them in the View (By calling the controller class when needed).
This is the basic theory of MVC...
Good luck!
I will give you a simple example of a car object that can be sold... this example sucks, but you can understand from it how MVC works...
?>