开发者

PHP MVC - Store User Variables in Controller or Model?

开发者 https://www.devze.com 2023-03-23 12:33 出处:网络
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, Sup

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 accoun开发者_StackOverflow中文版t 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.


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.


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

?>

0

精彩评论

暂无评论...
验证码 换一张
取 消