How do you pass values between PHP pages for MVC?

后端 未结 6 1337
南方客
南方客 2021-01-01 07:11

How do PHP programs pass values between model, view, and controller pages? For example, if a controller had an array, how does it pass that to the view?

EDIT:

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-01 07:24

    I struggled with this question myself. I had returned to php programming after nearly a decade. I ran into couple of issues deploying PHP MVC frameworks into an experimental server so I ended up building a simple MVC variant myself.

    In my simple design (front) controller interacts with the Model to populate a payload object and passes it to the view. Each view is implemented in a separate PHP file so controller uses include, to "load" the desired view. The view then can access the data via the designated name ($dataId) for the payload object.

    class Controller {
            public $model;
            ...
            public function display($viewId,$dataId,$data)
            {
              $url = $this->getViewURL($viewId);
              $this->loadView($url,$dataId,$data);              
            }
    
            public function getViewURL($key)
            {
                $url = 'view/list_view.php';
    
                if($key == 'create')
                {
                    $url = 'view/create_view.php';
                }
                else if($key == 'delete')
                {
                    $url = 'view/delete_view.php';
                }
    
    
                return $url;            
            }
    
            public function loadView($url,$variable_name, $data)
            {
                $$variable_name = $data;
                include $url;
            }
    }
    

提交回复
热议问题