General: Help on passing a layout to MVC structure (for ZendFramework)

Hello all,

Please have a look of both links here, that represent a layout structure of a given website:

The homepage:
http://www.help.nuvemk.com/mvc_zend/home_page.png

The secondary pages:
http://www.help.nuvemk.com/mvc_zend/secondary_pages.png

Newbie here, so don’t get to complicated on me. :slight_smile: Please. (only if you REALLY must).

I do have a lot of questions, but I believe they should be quick answered by anyone used to mvc structures and oo in general.

1) About the navigation:
1.1) Should we draw the navigation on our Layout?

1.2) Or should we repeat the code for each view?

1.3) Or a helper? If this generates to many discussion, please just advice one with restrictions.

1.3.1) If we do it on the layout, how can we have the proper
menu item highlighted if the user is on a given page ?"

2) About the pink zone:
It will have a list of latest news comming from the database.
This list of latest news will be available on ALL pages.

2.1) If we do it on the layout, is there an issue to have a controller
and a model for our layout?

2.2) Is there a third option more reliable?

About HOME and SECONDARY pages:
3) As we can see, the HOME PAGE (singular) and the Secundary Pages (plural),
are quite different on their look. How can we deal with this?
3.1) Should we have two layouts, one for the HOME PAGE and another for the
secondary pages?

3.2)
Or shouldn’t we have a layout for the home page at all,
since it’s a solo page, hence, a view will sufice?

4) About the majority of examples seen:
On the secondary pages, I believe we can’t simply have a layout with a header and a footer, and between them, have:

<?php echo $this->layout()->content; ?>

Like so many examples out there. Not just that.

Between the header and the footer we have not only our view specific stuff, but we also have a pink box that will be equal on every page, and a navigation that it’s between them as well.
4.1) I’m wondering if we can have:

<?php echo $this->layout()->content; ?>

but also:

<?php echo $this->layout()->bigheadings; ?>

Kind Regards,
Márcio

Hi, I would say best practice is to do a partial view for your layouts. Do a different layout for the main and the sub pages. This way you can pre plan for the future if you need to use yet again another different layout for whatever reason (e.g. promotion page, landing page). I would also recommend you buy the Zend Framework book from phpArchitects.

Hello,

Thanks for the info.
I have “Zend Framework 1.8 Web Application Development” (it’s quite advanced).
And “Easy PHP Websites with the Zend Framework”.
More accessible for me right now. I feel I need to practice a lot, by seeing layout sketches and try to figure out, what on that layout sketches should be a Zend Layout, a Zend View etc…
That’s why I’m asking those (the most specific I can) questions. If you can specify a little more regarding those questions above, I would be more then glad. :slight_smile:

Thanks a lot for your reply. :slight_smile:
Márcio

I’d use a view helper for the navigation. you can then either pass the current controller/action name to the view helper via the controller (perhaps in the preDispatch() method so its available in all your actions)

$this->view->urlParams = $this->_request->getParams();

then in the layout call:

<?=$this->myNavigationMenu($this->urlParams);?>

or you could grab an instance of the front controller in the view helper code (although this might not be ‘pure’ MVC)

Both of these will allow you to find the current action being called allowing you to highlight the current navigational item (or do a breadcrumb etc)

Here’s an example of a view helper you could use to grab some news articles and render them http://www.pastie.org/955606

There’s also Zend Navigation, although i’ve never used this.

If the text is going to be static i’d use a partial. if not i’d use a view helper. You could cache the contents of the view helper to cut down on db calls.

About HOME and SECONDARY pages:
3) As we can see, the HOME PAGE (singular) and the Secundary Pages (plural),
are quite different on their look. How can we deal with this?
3.1) Should we have two layouts, one for the HOME PAGE and another for the
secondary pages?

I’d have two separate layouts, just have $this->_helper->layout->setLayout(“homepage”); for your index action.

4) About the majority of examples seen:
On the secondary pages, I believe we can’t simply have a layout with a header and a footer, and between them, have:

<?php echo $this->layout()->content; ?>

Like so many examples out there. Not just that.

I’d use partials for your header and footer if you feel they would change, but for some cases i’d just leave in the layout as its only one file to change.

Between the header and the footer we have not only our view specific stuff, but we also have a pink box that will be equal on every page, and a navigation that it’s between them as well.
4.1) I’m wondering if we can have:

<?php echo $this->layout()->content; ?>

but also:

<?php echo $this->layout()->bigheadings; ?>

You can use layout placeholders, so in your example of the ‘big image that changes in each view’ i’d setup a placeholder in the layout such as


<img src="<?=$this->placeholder('bigImage');?>" />

then in each of your actions have

$this->view->placeholder(‘bigImage’)->set(“foo.jpg”);

HTH

Thanks a lot.

That will allow me to kick off this first MVC with Zend Experience.
Several concepts are completely new to me, I will investigate them.

Thanks a lot again, for sharing your thoughts on “how you will do something like this”.

Márcio

Good luck! One of the pros/cons of ZF is that there’s many different ways to perform the same thing, so it can be daunting.