tags:

views:

482

answers:

1

Hello I'm building my own PHP MVC framework. Not with the intention of using it. But I'm trying to learn PHP5 OO and the MVC design pattern.

I've read a lot of tutorials and got the basics working but now I'm stuck since things are getting more complicated.

My framework uses the following URL structure: /controller/action. Optionally followed by an inifinite number of variables, e.g. /product/view/1.

So far I got two separate controllers: page and product. I wan't to include them both in a single menu. I'm trying to establish the following menu structure. The corrosponding URL is between brackets.

  • Our company (/page/view/2)
    • Werkwijze (/page/view/3)
    • Staff (/page/view/4)
  • Our products (/product/index)
    • Bread (/product/category/1)
    • Banket (/product/category/2)
    • Cake (/product/category/3)
  • Contact (/page/view/5)

So basically I got a main menu and a sub menu. There are a few requirements I defined for the menu class:

  • The current item should have a different CSS class in the menu.
  • If the current item has a parent than that should have a different CSS class as well.
  • The menu should be expandable using all kinds of content a URL's.
  • And for bonus points: when I select a product from the Bread category. Let's say /product/view/1; then I'd like the 'Our products' and 'Bread' menu items to be highlighted as well.

I've got a copy of my 'framework' running here: http://www.eriknijland.nl/stackoverflow/. The content is in Dutch though and the menu is just static HTML.

The source code is available for download as well in the folder:

  • /stackoverflow/source/framework.zip
  • /stackoverflow/source/framework.sql

Any other comments on my code are welcome as well :P.

+1  A: 

All the code to build that should be in the view layer of the application. Of course, the framework should provide methods to know wich module and action are active, but the behavior of the menu should be implemented in the application and not in the framework.

Another option would be to design the framework so that it provides some helpers to build menus automatically. In this case, everything should be implemented within the view layer of the framework.

miguelSantirso
Well thanks... but how about a menu 'library class' (as part of the framework, not just for the application). And the controllers can add active menu-items for each menu.But then again. It's the view that needs the menu. I also got XML views planned, they don't need a menu. Or maybe the controller should only add the active items depending on the view.Just think out loud here...
Erik
You don't really see menu libraries or helpers in MVC frameworks. You want to be able to give the person using the framework full flexibility and customization for their menu. Remember, you're building a MVC framework here, not a CMS (which in that case, a menu library would be OK).
jimyi
@jimyi I'm not sure how to define framework or CMS. But I think my 'framework' is actually a CMS.Since almost all of my classses have custom functionality made especially for the demo site.My todo list for tomorrow:- create a template class- let the template class create the menu class- use controllers to notify the menu (trough the template class?) of active items- let the controller render the template- the template renders the menuPlease correct me if I'm wrong.
Erik
The controller should not need to communicate nothing to the view. The _only_ thing the controller should do is to implement the business logic. The view (the template class) is the one that should guess what module and actions are activated.However, I agree with jimyi: This is not responsability of a MVC framework.
miguelSantirso