1

I have seen all types of tutorials: View running controller, controller passing model to the view, controller setting setter of view.

An example: MVC for a reading of a news. The controller loads the Model. If the result of the model is 'false' I can call another method from another model containing different block.

The View class must be relevant to, View_found View_not_found?

if (model-> news === true) {
      $ comment = model-> comment ()
}

Would this code snippet be the responsibility of the controller or is it a rule that such logic should belong to business model?

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
Papa Charlie
  • 625
  • 8
  • 31

4 Answers4

0

If you want to find model News comment, you need to create Comment model and get it from your dataProvider and render to view.

Here is a part of controller action code :-). All logic must be in controller. Views is only render content parts.

UPDATED I recommend using here dataProvider. Model method (getter) can return DP object for model. It depends which framework you're using.

user1235098
  • 65
  • 1
  • 6
  • then have the controller set the attributes of view is correct: **in the controller...** `$view-> news = $model-> news;` correct? – Papa Charlie Mar 02 '12 at 08:58
0

Like said Rasmus Lerdorf

MVC is the current buzz in web application architectures. It comes from4event-driven desktop application design and doesn't fit into web application5design very well. But luckily nobody really knows what MVC means, so we can6call our presentation layer separation mechanism MVC and move on.

As a matter of fact, in web application, it is common to use "action oriented MVC" instead of "event MVC". You've got a controller that knows his model, and the view to apply. To display news, You must have a News class which is your model, a NewsProvider which deals with your database. Your controller compute the datas given by the database and the user and call the proper view.

artragis
  • 3,677
  • 1
  • 18
  • 30
0

in my experience, i tend to program the model and view as "blind". the model and view only receives parameters needed and then spits out what is needed. they should do minimal to no logic at all.

for the model it performs minor checks, like parameter type and validity and returns either a result or false. the controller does not know how the data is stored, where, why etc.

for the view, it should receive a string preferably through only one entry point, a function which will do the escape and echo. other than that, the controller should never echo to a page.

everything else is for the controller to digest like validation, calling what's needed, determine what is what. the controller sees everything:

//get from model, pass parameter - that's it
if (model-> news ('all')) {
    //manipulate data for result
    //get appropriate view
    view->parse(html); //pass html to view, that's it
} else {
    //manipulate data for no result
    //get appropriate view
    view->parse(html); //pass html to view, that's it
}
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Then my controller would look like? $model = new Model(); $model-> news( $param ); if( $model-> news === false ) { // get other content $model-> other_content(); loader( 'view_news_not_found' , $model-> other_content ); } else { // get comments news $model-> comment(); loader( 'view_news_found' , $model-> news , $model-> comment ); } – Papa Charlie Mar 02 '12 at 08:53
  • I'm using mysqli, my model makes the connection, it should call the function 'closeDB ()', the controller would be calling the method of model: `$model-> after()`, or should use `__destruct`? thank you :D – Papa Charlie Mar 02 '12 at 09:05
  • closing the connection is not necessarily needed, as sometimes you may not expect that there are further calls to the database you might do sometime later. – Joseph Mar 02 '12 at 10:45
0

The best practice is to keep business logic in Models

see below links for reference

http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices

ASP.NET MVC - Should business logic exist in controllers?

http://blogs.msdn.com/b/aspnetue/archive/2010/09/17/second_2d00_post.aspx

Community
  • 1
  • 1
Junaid
  • 2,084
  • 1
  • 20
  • 30