views:

307

answers:

5

Hi,

where, in your opinnion, should a breadcrumbs path be declared (in other words, in which letter of MVC)? So far I've been declaring it in Controllers, but I've recently started to work with CakePHP, where it is all made in Views and it surprised me.

I'm just curious of your opinions.

+1  A: 

Whenever you see words "logic" and "view" together you should start worrying. My vote is for Controller because breadcrumbs is typical example of application-level logic, so putting it to the view violates MVC in my opinion.

bsboris
+1  A: 

Breadcrumbs should be in the controller. I use CodeIgniter and do something like

$data['breadcrumb'][0] = array("title" => "Home", "alt" => "Home Page", "path" => "home/");
$data['breadcrumb'][1] = array("title" => "About", "alt" => "About Me", "path" => "home/about");

And then in the view loop through and display them as a list.

<ul class="breadcrumbs">
foreach($breadcrumb as $b)
{
    ?>
        <li><a href="<?php echo base_url(); ?>index.php/<?php echo $b['path'];?>" alt="<?php echo $b['alt']; ?>"><?php echo $b['title']; ?></a></li>
    <?php
}
</ul>

You can then also declare simple things like classes, current pages, etc. The only downside is you have to set the breadcrumbs in every page.

ALL logic should be in the controller. Access databases through the models, perform logic in the controller, and pass it to the view.

Simple stuff like

<?php echo ($loggedin)?"Logged in as" . $user->firstname:"Not logged in";?>

can be in the view. But you shouldn't be setting up complex flow patterns. Leave that to the controller. Views are cheap. You can have a half dozen slightly different views and no one will care. It's not like static HTML where you would have to maintain a half dozen pages.

Include common things in the views (like headers, footers, script files, javascript files, etc) and then let them be.

Josh K
That is actually wrong! The logic should be in the model not the controller. The model is not just for database process
AntonioCS
@Antonio: No, actually you're wrong. *Some* logic can be in the model, but it must be model specific. You wouldn't have a breadcrumb path in your model.
Josh K
@Josh: Yes you would. If it's processing data, changing data, creating new data, it's in the model. Controllers are just for setting up the correct view and calling the model. Please read up on the MVC model. Again, model is not the database!
AntonioCS
I would certainly prefer this logic in the controller over the model - the model generally shouldn't know about the view or view logic. However, you could have a ViewModel, in which case, the logic would be best there.
sydneyos
A: 

IMO, the breadcrumb relates to the set of controller actions taken to get to the current page or the action's place in the hierarchy of the site, depending on your interpretation. From that perspective, the controller seems the natural place to construct the data for it, though the rendering should occur in the view. In order for it to be completely generated in the view, you would need to either expose details about what controller action is being invoked by the view or have a fixed view-per-action model so that the breadcrumb for each could be precalculated. Moving the logic for computing the breadcrumb to the view itself seems to violate the separation of concerns in MVC and may preclude reusing views for different actions, which would violate DRY>

In my opinion, generating data for the breadcrumb is a cross-cutting concern in the controller, i.e., you have some shared code that runs regardless of the action that uses the url to construct the breadcrumb data. There is also some shared view code that takes the controller-supplied data and renders it consistent with the design.

Note that I'm speaking from a purely architectural perspective. I'm not familiar enough with CakePHP (or other PHP frameworks, for that matter) to even judge whether your observation about it is correct. From a pattern perspective, putting it in the controller seems like the right thing to do. In a given implementation, though, it might make sense to violate the pattern.

tvanfosson
A: 

I know this is late, but it's done in the model. Not the controller. The view would ask the model for the breadcrumb. Get it? It models the view :O. The controller is simply the application logic. Somebody said that breadcrumbs is typical of application logic, but it's not, it's atypical model logic, the breadcrumb is NOT the logic, it's part of the logic so it goes directly into the model. Somebody said that the model also does the data processing, this is wrong. The controller does that, if you save something, post something or want to action something, this is done in the controller. The model only setups the the view and in most cases would do the view queries such as pulling the breadcrumbs from the database. In theory you could, by application, depending on how you want, put everything into the controller like somebody else showed which is not atypical of a lot of applications but it's alright.

Jordon Bedwell
A: 
$html->addCrumb('Home', '/pages/home');
$html->addCrumb('Contacts', '/pages/contacts');

echo $html->getCrumbs('&raquo;', 'StartText');
Aziz