简体   繁体   English

如何在MVC的PHP页面之间传递值?

[英]How do you pass values between PHP pages for MVC?

How do PHP programs pass values between model, view, and controller pages? PHP程序如何在模型,视图和控制器页面之间传递值? For example, if a controller had an array, how does it pass that to the view? 例如,如果控制器有一个数组,它如何将它传递给视图?

EDIT: 编辑:

Thank your answers. 谢谢你的回答。 I see a couple of them stating the components are in the same page, but when I look at something like CodeIgniter, I see three separate PHP pages for model, view, and controller. 我看到其中几个说明组件在同一页面,但是当我看到类似CodeIgniter的东西时,我看到三个单独的PHP页面用于模型,视图和控制器。

Usually your controller will create a view object and when it creates that view object it passes the information. 通常,您的控制器将创建一个视图对象,当它创建该视图对象时,它会传递信息。

<?php

class Controller {
    public function __construct() {
        $arr = array("a","b","c");

        $myView = new View($arr);
    }
}

class View {

    private $content;

    public function __construct($content) {
        $this->content = $content;
        include_once('myPage.inc.html');
    }
}


?>

CodeIgniter, like most PHP MVC frameworks, comes with a View "engine". 与大多数PHP MVC框架一样,CodeIgniter附带一个View“引擎”。 In otherwords, there's a class in the framework responsible for loading and transferring data from your controller to the view. 换句话说,框架中有一个类负责将数据从控制器加载到视图中。

Specific to CodeIgniter, those calls look like this: 特定于CodeIgniter,这些调用如下所示:

$data = array(
           'title' => 'My Title',
           'heading' => 'My Heading',
           'message' => 'My Message'
      );

$this->load->view('blogview', $data);

Your view would then be a separate php page, in this case blogview.php . 您的视图将是一个单独的php页面,在本例中为blogview.php It may look like this: 它可能看起来像这样:

<html>
   <head><title><?= $title ?></title></head>
   <body>
      <h2><?= $heading ?></h2>
      <p><?= $message ?></p>
   </body>
</html>

The part responsible for transferring the data between the controller and the view is the View engine (or class) internal to CodeIgniter. 负责在控制器和视图之间传输数据的部分是CodeIgniter内部的View引擎(或类)。 It takes that array from the controller and deserializes it for the view. 它从控制器获取该数组并为视图反序列化它。

So why two separate files? 那为什么两个单独的文件? The MVC paradigm is such that, by design, you want your business logic separated from your UI. MVC范例是这样的,按照设计,您希望将业务逻辑与UI分离。 You don't need to incorporate the Model part of MVC if you aren't doing much with a database. 如果您没有对数据库做太多工作,则无需合并MVC的Model部分。 But it's there when you want to further separate the data access portions of your code from your business logic (the stuff that manipulates the data for the user). 但是当你想要进一步将代码的数据访问部分与业务逻辑(为用户操作数据的东西)分开时,就会出现这种情况。 In that case, you've got your 3 separate files; 在这种情况下,你有3个单独的文件; model, controller, view. 模型,控制器,视图。

So no, the components aren't all in the same page, though they certainly could be. 所以不,组件并不都在同一页面,尽管它们当然可以。 But by structuring your program using MVC, you separate the operations of your application cleanly and neatly. 但是通过使用MVC构建程序,您可以干净利落地分离应用程序的操作。 That way, you can work in one layer at a time without effecting the other layers. 这样,您可以一次在一个图层中工作,而不会影响其他图层。 Your CSS guy could be making the site look pretty while you figure out why the user can't log in, for example. 例如,当你弄清楚用户无法登录的原因时,你的CSS人员可能会使网站看起来很漂亮。

I hope Johnny has long ago found the answer that I'm searching for now, but I can supply an insight on how to do it unsatisfactorily! 我希望Johnny很久以前找到了我正在寻找的答案,但是我可以提供一个如何做到这一点令人不满意的见解! You can pass from one info from one file (say a 'view' component that collects data input by a site user) to another file (say a 'model' component that checks/validates the form data and either sends it back to the form for revision or stores it in a database). 您可以从一个文件传递一个信息(比如收集网站用户输入的数据的“视图”组件)到另一个文件(比如一个'模型'组件,它检查/验证表单数据并将其发送回表单用于修订或将其存储在数据库中)。 Form values can be sent via the POST or GET arrays and the 'action' attribute determines which file receives the data eg 表单值可以通过POST或GET数组发送,'action'属性确定哪个文件接收数据,例如

<form action = 'form-exec.php' method = 'POST'>
---some set of  possible user inputs goes here
<input name='submit' type='submit' value='Go'>
</form>

After processing, you may well want to return some data to the form - such as the user input values that need to be amended, or a success message. 处理完毕后,您可能希望将某些数据返回到表单 - 例如需要修改的用户输入值或成功消息。 You can store that data in session variables and recover it from those variables on the 'view' page. 您可以将该数据存储在会话变量中,并从“视图”页面上的这些变量中恢复它。 So, how do you get back to the 'view' page? 那么,你如何回到“视图”页面? Now, I'm here because I have seen this used and have used it myself but was pretty horrified to read that, apparently, the transfer goes via a dog-slow internet http request - even when the transfer is to a file in the same directory on the server! 现在,我在这里,因为我已经看到这个使用过并且自己使用了它但是非常惊恐地看到,显然,转移是通过狗慢的互联网http请求 - 即使转移到同一个文件服务器上的目录! Okay, so the UNSATISFACTORY way to get back to your 'view' component is a couple of PHP lines like this: 好的,所以回到你的'view'组件的UNSATISFACTORY方法就是这样的几行PHP:

header("location: theinputform.php");
    exit;

It does work, but there must be a better way, surely? 它确实有效,但肯定有更好的方法吗? I have seen a 'rant' from someone saying that you should just use an include, but I can't understand how to do the reasonably everyday thing of going back to the top of a page and recreating it according to the new conditions. 我看到有人说你应该只使用一个包含的“咆哮”,但我无法理解如何做到日常合理的事情,回到页面顶部并根据新的条件重新创建它。 For example, if I just logged a User in I don't want to be showing an invitation to login this time around. 例如,如果我刚刚登录了一个用户,我不希望这次显示登录邀请。 Includes are the no-brainer for conditionally ADDING something to a page, but I haven't found a better way to simply return to the top than the above. 包含是有条件地添加到页面的东西是明智的,但我没有找到一个更好的方式简单地返回顶部而不是上面。 Does anyone have any insight or knowledge on this? 有没有人对此有任何见解或知识? As the OP states, Johnny and I want to do the same as a MVC but are not using a MVC framework of any kind. 正如OP所述,Johnny和我想要像MVC一样做但不使用任何类型的MVC框架。 There are several books and tutorials out there that simply use the above 'headers' method, btw. 有几本书和教程只是使用上面的'标题'方法,顺便说一句。

Okay, edit: You get from a form (ie a 'view' page) to the form processing using the form's 'action' attribute, as above. 好的,编辑:您可以使用表单的“action”属性从表单(即“查看”页面)到表单处理,如上所述。 If the 'model' page outputs no html and just 'does things', you can simply include the 'view' page that you wish to go to (or return to) on completion. 如果'model'页面没有输出html而只输出'做事',你可以简单地在完成后包含你想去(或返回)的'view'页面。 I think so, anyway! 无论如何,我是这么认为的! Just about to try that... 即将尝试......

I struggled with this question myself. 我自己也在努力解决这个问题。 I had returned to php programming after nearly a decade. 近十年后我回到了php编程。 I ran into couple of issues deploying PHP MVC frameworks into an experimental server so I ended up building a simple MVC variant myself. 我遇到了几个将PHP MVC框架部署到实验服务器的问题,所以我最终自己构建了一个简单的MVC变体。

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. 每个视图都在一个单独的PHP文件中实现,因此控制器使用include来“加载”所需的视图。 The view then can access the data via the designated name ($dataId) for the payload object. 然后,视图可以通过指定名称($ dataId)访问有效负载对象的数据。

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

model, view, and controller are not separate pages but one page. 模型,视图和控制器不是单独的页面而是一页。
So, no need to "pass" anything 所以,不需要“通过”任何东西

u can use a template engine like smarty to seperate views from controllers and models. 你可以使用像smarty这样的模板引擎来分离控制器和模型中的视图。

and pass variables to your template using assign method of smarty for example. 并使用smarty的assign方法将变量传递给模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM