PHP MVC - 将用户变量存储在控制器或模型中?

发布于 2024-11-27 00:44:01 字数 155 浏览 3 评论 0原文

在控制器或模型中存储变量的最佳实践是什么? 例如,当脚本执行时。它从会话中获取用户 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零時差 2024-12-04 00:44:01

在 PHP 中,考虑真正的 MVC 模型有点奇怪,因为您的模型、视图和控制器可以访问 $_SESSION。

例如,如果您要让用户登录,您的模型将执行以下操作:

class Model{
    ...
    static function login($username, $password){
        $result = Model::getUser($username, $password);

        if(empty($result)){
            return false;
        }
        else
        {
            $_SESSION['userid'] = $result['id'];
            // Assign other information you think you'll need in the session here
        }
    }

    static function loggedIn(){
        if(isset($_SESSION['userid']){
            return true;
        }
        else
        {
            return false;
        }
    }

    static function getAttribute($attr){
        return $_SESSION[$attr];
    }
    ...
}

class Controller{
    function someFxn(){
        $userInfo = Model::getAttribute('someAttr');
    }
}

显然必须花费这段代码,但它应该正确显示概念。我还在模型中使用了静态函数,但您可以使模型成为一个对象。


我的问题是我在模型中的哪里存储这些设置,或者将其传回控制器,控制器将存储这些设置?

根据您想要的方式,您可以每次通过模型从数据库中获取设置,也可以将它们存储在会话中。将内容存储在 $_SESSION 中将使您减少数据库调用。实际上,模型操作 $_SESSION 或数据库。如果您的模型特定于某些事物(您可以创建自己的用户模型),那么您可以实例化该对象并将您的信息存储在私有成员中。

控制器的重点是从模型中获取信息,然后相应地渲染页面。实际上,MVC 数据流是这样工作的:

  1. 向控制器发出请求,
  2. 控制器从模型中获取信息
    (这是可选的,也许控制器不需要模型中的任何内容)
  3. 模型将信息返回给控制器
    (如果您在上一步中提出了请求,则会发生这种情况)
  4. 控制器传递适当的信息以进行查看。

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:

class Model{
    ...
    static function login($username, $password){
        $result = Model::getUser($username, $password);

        if(empty($result)){
            return false;
        }
        else
        {
            $_SESSION['userid'] = $result['id'];
            // Assign other information you think you'll need in the session here
        }
    }

    static function loggedIn(){
        if(isset($_SESSION['userid']){
            return true;
        }
        else
        {
            return false;
        }
    }

    static function getAttribute($attr){
        return $_SESSION[$attr];
    }
    ...
}

class Controller{
    function someFxn(){
        $userInfo = Model::getAttribute('someAttr');
    }
}

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.


My questions is where do i store these settings, in the Model, or pass it back to the controller, and the controller will store these settings?

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:

  1. Request is made to controller
  2. Controller gets information form model
    (this is optional, maybe the controller doesn't need anything from the model)
  3. Model returns information to controller
    (Happens if you made a request form the previous step)
  4. Controller passes appropriate information to view.
凉城凉梦凉人心 2024-12-04 00:44:01

您将它们存储在模型中(从数据库中获取它们),使用控制器拉取它们(在页面加载时),然后在视图中显示它们的结果(通过在需要时调用控制器类)。

这就是MVC的基本理论...

祝你好运!

我会给你一个可以出售的汽车对象的简单例子...这个例子很糟糕,但是你可以从中了解 MVC 是如何工作的...

<?
// Data
class Car
{
    private  $_color;

    public function setColor($newC)
    {
        $this->_color = $newC;
    }
    public function getColor()
    {
        return $this->_color;
    }

    private  $_maxSpeed

    public function setMaxSpeed($newMS)
    {
        $this->_maxSpeed = $newMS;
    }
    public function getMaxSpeed()
    {
        return $this->maxSpeed;
    }
}

// Example
$car = new Car();
$car->setColor($dbInfo['color']);
$car->setMaxSpeed($dbInfo['maxSpeed']);

// Controller

class Sales
{
    . . .
    public function SaleCar(Costumer $costumer, Car $car, $quantity)
    {
        if($car->getColor() == "red") // Red is expensive color...
            $car->MultiplyPriceBy(1.5); // Just an example...
        else
            $car->SubsetQuantityBy($quantity); // The car has quantity propery as well... and so on...

        $costumer->setPaymentType("Credit-card");
        . . .
        $costumer->Pay($quantity * $car->getPrice());

        return $finalPrice; // $quantity * $car->getPrice()
    }
    . . .
}

// View
class SalesPanel
{
    . . .
        public function output()
        {
            foreach($this->cars as $car)
            {
                if(in_array($car->getID(), $_POST['car_id']))
                    Sales->SaleCar(Costumer::GetCostumerFromID($_SESSION['uid']), $car, $_POST['quanityty']);
            }

            $output = . . .
            $output .= "Car model GHi675 old by . . . "; // Get info from controller
        }
    . . .
}

?>

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...

<?
// Data
class Car
{
    private  $_color;

    public function setColor($newC)
    {
        $this->_color = $newC;
    }
    public function getColor()
    {
        return $this->_color;
    }

    private  $_maxSpeed

    public function setMaxSpeed($newMS)
    {
        $this->_maxSpeed = $newMS;
    }
    public function getMaxSpeed()
    {
        return $this->maxSpeed;
    }
}

// Example
$car = new Car();
$car->setColor($dbInfo['color']);
$car->setMaxSpeed($dbInfo['maxSpeed']);

// Controller

class Sales
{
    . . .
    public function SaleCar(Costumer $costumer, Car $car, $quantity)
    {
        if($car->getColor() == "red") // Red is expensive color...
            $car->MultiplyPriceBy(1.5); // Just an example...
        else
            $car->SubsetQuantityBy($quantity); // The car has quantity propery as well... and so on...

        $costumer->setPaymentType("Credit-card");
        . . .
        $costumer->Pay($quantity * $car->getPrice());

        return $finalPrice; // $quantity * $car->getPrice()
    }
    . . .
}

// View
class SalesPanel
{
    . . .
        public function output()
        {
            foreach($this->cars as $car)
            {
                if(in_array($car->getID(), $_POST['car_id']))
                    Sales->SaleCar(Costumer::GetCostumerFromID($_SESSION['uid']), $car, $_POST['quanityty']);
            }

            $output = . . .
            $output .= "Car model GHi675 old by . . . "; // Get info from controller
        }
    . . .
}

?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文