Breadcrumbs Preparation In Controller Vs. View

HI,

In all the documentation and examples I’ve seen (well, probably most, at least), breadcrumbs are rendered in the main layout file and the ‘data’ to the breadcrumbs widget, in the form of an array, is prepared in the specific view file being used.

Now, I was wondering why are the breadcrumbs being prepared in a view file? It seems (to me) that a more proper place for it would be in the action that is in charge of rendering that view file. View file should contain minimal php logic and since the breadcrumbs is not even rendered there, why not locate the preparation of the controller.links array in the controller?

I’m asking to see if I’m missing something… .

TIA,

Boaz

I agree with you - it’s better to place it in controller action, from my point of view.

In a few cases is difficult to deside which is the correct place of something.

for example the breadcrumbs may has translated text

So according to the MVC the appropriate place is the view

On the other hand if you want to take the texts from model then the data must be come from model through the controller.

So there is not stricted way to deside which is the best place for any part code.

I tried both cases. I still prefer put breadcrumbs inside views files with page title and other things related to the presentation of a view.

Guys thanks for sharing your opinions on this.

I see indeed that there is no conclusive answer and probably not many consideration that slipped my thought on this.

Compiling your feedback, my current thoughts are:

  • Calculating the links array, which involves using createUrl and possibly other calculations, as well as the whole determination of breadcrumbs, is a kind of logic that fits better in an action method than in a view file.
  • Controlling visual aspects of the page, including translation of breadcrumb strings, right along setting title and other front-end-related issues, seems logical to be made in the view part of the application (=view files).

In my personal taste and for the project I’m currently working on, the first bullet weight is greater than the second bullet so I prefer setting breadcrumbs in the action method.