How do you pass values between PHP pages for MVC?

后端 未结 6 1324
南方客
南方客 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:14

    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! 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 e.g.

    ---some set of possible user inputs goes here

    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! Okay, so the UNSATISFACTORY way to get back to your 'view' component is a couple of PHP lines like this:

    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. There are several books and tutorials out there that simply use the above 'headers' method, btw.

    Okay, edit: You get from a form (i.e. a 'view' page) to the form processing using the form's 'action' attribute, as above. 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. I think so, anyway! Just about to try that...

提交回复
热议问题