views:

38

answers:

1

Good day!

I need suggestions how to solve one case while developing app with CakePHP...

I got layout created and named default.ctp. In that layout I got sidebar with some blocks and there're some statistics taken from the database.

Now my solution: I just created model called Sidebar.php and there're some functions, then I set up data in controller to display it in layout. Is this the best solution? As far I know, I will have to re-set every data in every controller, so need suggestions how to solve that.

Thanks for your suggestions!

+1  A: 

Bear in mind that this is coming from a 10,000' level - I know nothing of your particular circumstances, but IMO it's not the best solution. I say that because you've created a model that represents a presentation component. If it were me, I'd probably look at using an element for display. Displaying dynamic components gets a little dodgy, but can be done without violating the MVC "covenant".

Your models should represent your domain entities (you've mentioned nothing about what your stats represent, so I won't offer any specific examples), not how they're presented.

Rob Wilkerson
Rob, thanks for your reply and time spent on writing it. I know that sometimes I'm a bit hard to understand, sorry for that, English is not my native language so I'm better in thinking in English than writing. I created a model, model takes data from database. In controller I use $this->set('stats', $this->Sidebar->generate_stats() ); to sent array to the layout. In layout I use foreach loop to display it. Is it, in your opinion a violation of MVC convention? As far I understand MVC conventions, model should be used to the login part of the app. Am I right?
Gilles
No, I don't think it's a violation of MVC (you're retrieving data from your model, setting it in your controller and accessing it in your view), per se. Perhaps the only thing it "violates" is my preference. :-) If the stats were related to football team stats, for example, I'd prefer that they be generated by the Team model since that's what they relate to. "Sidebar" is a presentational construct.
Rob Wilkerson

related questions