PHP MVC 最佳实践 - 将会话变量从控制器传递到模型类或直接在模型中访问
我们的开发团队正在讨论一般最佳实践: 是直接从模型类中的函数访问会话变量更好,还是将会话变量从控制器作为参数传递给模型类中的函数更好。看下面的两个例子:
直接从模型类访问会话变量以在查询中使用:
class MyModel {
public function getUserPrefs($userID) {
$this->query("SELECT * FROM my_table WHERE id=$_SESSION['userID']");
}
}
或者将会话变量作为函数参数从控制器传递到模型类中的函数:
class MyController {
public function displayUsers() {
$this->model->getUserPrefs($_SESSION['userID']);
}
}
class MyModel {
public function getUserPrefs($userID) {
$this->query("SELECT * FROM my_table WHERE id=$userID");
}
}
将其传递到模型的原因控制器是这样的,所以引用的所有数据都来自一个入口点,即控制器。
什么被认为是更好的做法?
Our development team is debating a general best practice:
Is it better to access a session variable directly from a function in a model class or pass the session variable from the controller as an argument to a function in the model class. Look at the two examples below:
Accessing session variable directly from the model class to use in a query:
class MyModel {
public function getUserPrefs($userID) {
$this->query("SELECT * FROM my_table WHERE id=$_SESSION['userID']");
}
}
Or pass the session variable from the controller to a function in the model class as a function argument:
class MyController {
public function displayUsers() {
$this->model->getUserPrefs($_SESSION['userID']);
}
}
class MyModel {
public function getUserPrefs($userID) {
$this->query("SELECT * FROM my_table WHERE id=$userID");
}
}
The reasoning for passing it to the model from the controller is so all data that is referenced is coming from one point of entry, that being the controller.
What is recognized as a better practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第二个版本(将 $_SESSION['userId'] 作为参数传递给方法)会产生更加解耦的类,因此更加灵活。随它去吧。
The second version (passing $_SESSION['userId'] as an argument to the method) results in a more decoupled class, and therefore more flexible. Go with it.
您永远不想在模型中包含会话变量。您应该始终将这些变量作为参数传递给模型中的函数。这也使您的代码更具可扩展性和灵活性。考虑一个通过 ID 获取用户的模型。您可以编写如下函数:
但是,如果您现在要构建具有用户查找功能的管理功能怎么办?您的模型被硬编码为使用会话的 user_id,但您希望能够传递您自己的 id。你会更好:
并且在你的控制器中
但是,在这种情况下,我真的会考虑让你的代码更加灵活:
这允许你在一个查询中获得多个用户!这更有效率。那么,在您看来:
您还应该考虑为 User 使用 singleton 类来限制数据库负载。创建一个名为 CurrentUser 的不变实例类(例如),如下所示:
这是单例类的一个非常基本的示例,并且缺少很多内容。如果您想了解有关单例类的更多信息,请发布另一个问题。
You NEVER want to have session variables in your model. You should always pass these variables as parameters to the function in the model. This also makes your code more expandable and flexible. Consider a model that gets a user by their id. You may write a function like:
However, what if you now what to build an admin functionality with a user lookup feature? Your model is hardcoded to use the session's user_id, but you want to be able to pass your own id. You would be better off:
and in your controller
In this case, however, I would really consider making your code even MORE flexible:
This allows you to get multiple users in ONE query! Which is way more efficient. Then, in your view:
You should also consider using a singelton class for a User to limit database load. Create a non-changing instance class called CurrentUser (for example) like:
This is a really basic example of a singleton class and is missing a lot of stuff. If you want to know more about singleton classes, post another question.
请记住,“会话”只是另一种模型。然而,第一种方法是不可接受的 - 如果您想获取其他用户的偏好只是为了将它们与某些内容进行比较,该怎么办?使用第二种方法。
Keep in mind that "session" is just yet another model. However first approach is unacceptable - what if you would like to fetch another users preferences just to compare them with something? Use the second approach.
我同意赛斯的观点。 “您还应该考虑为用户使用单例类来限制数据库负载。创建一个名为 CurrentUser 的不变实例类”。
在我的伪 MVC 应用程序中,我有类 User(表示当前用户),其中包含用于会话、获取用户信息、角色等的方法,以及类 Member(表示任何给定用户),其中包含用于注册新用户、获取/更新其属性等的方法但与会话无关,例如。此外,它是单例场景,因此当前用户是静态的,不需要与数据库进行太多交互。
因此,就我而言,我的控制器和视图调用 User 方法,例如
User::getId()
或User::getGroups()
。I agree with Seth re. "You should also consider using a singelton class for a User to limit database load. Create a non-changing instance class called CurrentUser".
In my pseudo-MVC app I have class User (meaning current user) with methods for session, getting user info, roles etc., and class Member (meaning any given user) with methods for registering new users, getting/updating their properties etc but nothing to do with sessions, for example. Also, it is a singleton scenario, so current user is static and does not require much interaction with the DB.
So in my case, my controller and views make calls to User methods, e.g.
User::getId()
orUser::getGroups()
.