Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass values from controller to view in PHP without frameworks? [closed]

I am new in PHP and have read about MVC. Currently, I am not using any frameworks and will like to know how can I push data from the controller to the view.

Background:

  1. htmlForm1.php -> Get inputs and pass data to DoWork.php to process
  2. DoWork.php -> Do work and pass to htmlForm2.php to display
  3. htmlForm2.php -> Display results

I understand that I obtain the values (from htmlForm1.php) in DoWork.php through _GET. My question is how do I get the values (from DoWork.php) in htmlForm2.php?

I have thought of using session, but is this the only solution? I do not require this data to be persistent after htmlForm2.php

like image 930
Wee Avatar asked Jan 26 '13 03:01

Wee


2 Answers

Technically speaking the view could request model data directly.

MVC

The dotted line that connects the model to the view is exactly that connection.

But the MVC web oriented is different from the common MVC approach because of its per-request execution. When speaking about MVC outside the web, the view has a lot of other features rather than just retrieving data and print it: the view can connect directly to the model, request data after it is loaded, and also trigger actions in the controller (on a specific event in the view). Things that are near impossible to achieve with HTML only (Javascript could be turned off therefore cannot be used) without actually loading a complete new request and get a complete new response. The HTML part of an application, as of right now, is stuck at being a dumb template because it was designed to be the static output of the request.

Most of the PHP web frameworks treats the view as a template, and I think (IMHO) that that's the best and clearer approach for this context. In that way if you send a view as response you could send variables to the view from the controller with a simple:

$view = new View('view/myView.php');
$view->assign('a' => 'b');

and then inside the view class simply extract() the variables in the array of the assigned vars before include the actual view file.

like image 85
Shoe Avatar answered Oct 26 '22 22:10

Shoe


The only information, that controller should be pushing to the view instance, would be user input (and even then only in rare cases).

Instead the views should be requesting information from model layer through the services. The controller is mostly responsible for changing the state of mode layer, based on user input. That also should be done through services.

To accomplish this the controller and view must have access to the same model layer. This is usually done by injecting some sort of ServiceFactory in both instances. The factory then makes sure that every service is instantiated only once, without making the code dependent on global state.

P.S.: The views are not dumb templates. They are classes which are responsible for UI logic in the application. They are what creates a response for a user, when MVC is used in context of Web.

P.P.S.: Model is not a class or an object. It is one of two layers in MVC. The other layer is the presentation layer, where controller, views and tempaltes are. The model layer consists mostly of three major groups, each responsible for one specific aspect of the layer: application logic, domain/business logic and storage logic.

like image 29
tereško Avatar answered Oct 26 '22 23:10

tereško