-1

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.

2
  • 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 database Commented May 25, 2011 at 18:51
  • 2
    possible duplicate of How to connect controller to view in PHP OOP ? Commented May 25, 2011 at 18:52

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

But i have not use any Class Or Object inside the view file. its just like a simple html file.
It shouldn't have any class defined in it. Don't think of it as a "view file," think of it as a template that gets rendered by a View object. You have to write a View class that will take as input, the template and any possible variables that it needs, and output the resulting HTML. It might be as simple as calling readfile().
0

Controler can include view and model file.

When controller is called by url, get or put contents(in a db) with models methods, and another that send data to view, and view is output for browser.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.