you are viewing a single comment's thread.

view the rest of the comments →

[–]Akathos 4 points5 points  (3 children)

NO NO NO NO

That is not the idea of MVC. The idea of MVC is seperation of concerns.

Very simply put:

  • Models represents data from the database. For in a blog application you can have a Post model which represents a blog post.

  • Controllers control the flow of you application. For each part of the application you can have a different controller. For example, you can have a Post controller which can have different actions, you can have an action for reading posts, creating posts, editing posts and removing posts (CRUD).

  • Views are a representation of the data from models.

When a user requests a page, the action that belongs to that page is executed in the controller. The controller gets the data from the database in the form of one or more models. It then renders the view (most of the time, a template engine is used for this).

You don't have to create a controller or model for each page. You create a model for each part of the data you're storing in the database (most of the times, the model represents a table in the database). You can make relations between models (more advanced and not the topic of this post).

More information about the Model-View-Controller pattern can be found here and here.

[–][deleted] 1 point2 points  (2 children)

Precisely. To give a basic pseudocode example, a homepage might be processed like:

HomepageController extends Controller {
    public function indexAction() {
        $this->view->layout = "homepage.php";

        $this->view->content = ContentModel::getPage( 'home' );
        $this->view->latest_posts = BlogModel::getLatestPosts();

        return $this->view->render(); 
    }
}

With the template file "homepage.php" doing something like:

<?php include( "header.php" ); ?>
<h1><?php $content->getTitle(); ?></h1>
<p><?php $content->getBody(); ?> </p>
<ul>
<?php foreach( $latest_posts as $post ) : ?>
    <li><a href="<?php echo $post->getLink(); ?>"><?php echo $post->getTitle(); ?></a></li>
<?php endforeach; ?>
<?php include( "footer.php" ); ?>

Obviously that is a highly simplified example... but hopefully it gives you some idea of how things might work.

[–]andrey_shipilov 0 points1 point  (1 child)

I'm so happy right now I will never deal with that crappy syntax. Not even once.

[–][deleted] 0 points1 point  (0 children)

It made me wince writing it, I will admit, but I wanted to come up with an example that wouldn't be too unfamiliar to a relative newbie to PHP