Craig Wilson
Craig Wilson

Reputation: 461

PHP MVC, partial views?

I'm building a small PHP MVC, and I've hit a wall with a small area of the coding. I think I need "partial views", but I might be able to achieve something with the existing code.

My controller in it's simplest form currently:

The view(s) currently:

Everything is working great, however the code is now getting fairly large in both the controllers and views, and I've reached a situation where I need to include "modules" or sub views inside the main views (1 and 2).

For example if view2 is loaded, then I need to display (as part of the view2) another view, say a 3 part sign up registration form. This registration form comes with it's own controller file too.

I can do something like this, which will work and give you some idea of what I am trying to do, but I acknowledge this breaks MVC design patterns.

View

<div id="mydiv">Some content</div>
<div id="mysignup"> <?php include('controller_signup.php'); ?></div>
<div id="content"> The views main page content </div>

The view is then pulled in from the controller, in the right place.

As mentioned, I think I need to use partial views, however most of the info I've found is for ASP, and I'm slightly lost!

I would have thought this is a fairly common problem, so I assume there is an obvious solution!

Thanks!

Upvotes: 2

Views: 15830

Answers (2)

laurent
laurent

Reputation: 90873

Can't say I agree with tereško. Having the presentation logic in a separate view class might seem more proper, however it does add another layer of complexity. In many cases, this additional layer is not necessary - often a view can directly render whatever model or data you inject. So you end up with empty View classes that only pass the model from the controller to the template.

I'd say, Keep It Simple, you can do pretty complex (and maintainable) websites with simple MVC, without introducing the notion of templates.

To handle subviews, you can simply have the main view inject data into the subviews, this should be transparent to the controller (who just provide the main data without caring how it should be rendered). So in practice, you could have something like this:

Controller:

public function someAction() {
    // ...
    $view = new View('name');
    $view->data = $someList;
}

View:

<?php foreach ($someList as $item): ?>
    <?php echo (new View('subview', $item))->render();
<?php endforeach; ?>

Upvotes: 2

tereško
tereško

Reputation: 58454

The root of you problem is fact that you do not have views. What you call a "view" is a actually a template. This in turn forces the presentation logic in the controller.

In proper MVC views are instances, which contain all of presentation logic. They acquire information from model layer and then, based in data, choose how to display this information. Each view manipulates multiple templates.

Also, it seems that your controller has gained extra responsibilities. It is supposed to change the state of model layer and current view, instead of rendering templates.

Upvotes: 5

Related Questions