Sunday, August 1, 2010

URLs in Models?

I am faced with a bit of a dilemma and I would welcome advice from
experienced Bakers.

I have a database called screens which uses the tree behaviour to
represents the structure of my website. So the home page will have a
number of child pages and each will have children in a hirerarchical
structure. (I find this a very useful way to build a simple website
quickly where each page has editable text but otherwise has the same
format.)

I am going through a refactoring phase at the moment and I have
created a helper class called Links which I use to build generic link
related html snippets, such as a linkbars. The linkBar function
automatically builds in the logic to identify first, last and
alternate rows in a link bar to allow hooks into CSS etc.

I want to be able to use this function from any controller
generically. The linkBar function takes an array which contains, for
each link, the title and URL to be used to create a structure like:

<ul>
<li><a href='URL'>title</a></li>
<li><a href='URL'>title</a></li>
<li><a href='URL'>title</a></li>
<li><a href='URL'>title</a></li>
</ul>

A common use of all this is to create a child linkbar which contains a
link to all all of the pages directly below this one in the
hierarchy. To do this I need to go to the model to get all the
children.

In order to avoid the inefficiency of getting the child data and then
having to process it again to create the URLs for my linkBar function,
I am tempted to code the getChildren function in the model to return
an array in the exact format for the linkBar function, but this means
that the model has to create the URLs which go in the array.

The URLs have the form 'screens/view/screenname' (although that is
determined by the routes.php configuration and I would prefer not to
hard code them like this.)

This immediately felt wrong. Surely models shouldn't be playing
around with URLs? This sort of code would normally be in the
controller, or possibly the view.

As I see it I am faced with the following options:

1. Do all the work in the model. Most efficient but architecturally
against the MVC philosophy.
2. Return the basic data (screenname) from the model and then create
the URL in the controller. Means more moving around of data so less
efficient but seems logical.
3. Calculate the URLs at the view stage. This seems wholly wrong to
me. I should do all the work before the view and then just pass data
to the view. The most complex logic in the view (I think) should be a
loop.

What to people think, both about this particular problem and the more
general question about how to get the right balance between efficiency
and architectural purity?

All responses welcome.

Thomas (Auspice).

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: