I am using PHP MVC pattern without any Framework, right now i have a view file with some data to be inserted into it, which will be save into the database. now my problem is how to connect the view file to the controller or Model.
-
right now i have connect the view file through the controller, but now i need how to connect the view file into the controller, because i need to save the view file data into the databaseuseCase– useCase2011-05-25 18:51:13 +00:00Commented May 25, 2011 at 18:51
-
2possible duplicate of How to connect controller to view in PHP OOP ?Cfreak– Cfreak2011-05-25 18:52:14 +00:00Commented May 25, 2011 at 18:52
3 Answers
The view shouldn't save anything to the database, that's the job of the model. The view is for rendering only. Typically, you'll instantiate a view object in your controller, pass it the data you want to render, then call some render method. Perhaps something like this:
$view = new View();
$view->setTemplate('/path/to/file');
$view->setValues(array(
'key1' => 'value1',
'key2' => 'value2',
));
$view->render();
If you want to save the data in the database, that's got nothing to do with the view. You might have something like this:
$model = new Model();
$model->setValues(array(
'key1' => 'value1',
'key2' => 'value2',
));
$model->save();
$view = new View();
$view->setTemplate('path/to/file');
$view->setValues($model->getValues());
$view->render();
2 Comments
The controller takes the data from the view and passes it along into the model. The model handles the persistence. Also in the HTTP world I would say the controller takes the data from the request, not from the view directly, but these are implementation details.
You just want to make sure that the model does not depend in any way on the view. This is one of the main rules in MVC.