MVC conundrum: How can I solve this problem and keep logic out of the view?
I'm building a site with CakePHP, but this question is more about solving an MVC problem than it is a CakePHP problem.
I have a User Model and a Group Model. This is a HABTM relationship, so different users may belong to multiple groups. One controller on my website handles the blog. It has many methods, but all of the views share a sidebar element with things like related posts, etc. I want to add some links to the sidebar of the blog that only a user开发者_StackOverflow社区 who belongs to the Admin group can see.
At first I decided to just loop in the view element through each group that the user belongs-to, and if the Admin group is found, echo the links and break the loop.
But this seems to break the MVC pattern. Is there a better way?
Just implement an isAdmin()
boolean method in the User model which will encapsulate the permission checking logic, then invoke it from a view to check whether a given user is an admin or not.
As a rule of thumb, keep all the business logic in the models.
精彩评论