model-view-controllernavigationrole-based

Generating Navigation for different user types, MVC, PHP


I have this idea of generating an array of user-links that will depend on user-roles.

The user can be a student or an admin.

What I have in mind is use a foreach loop to generate a list of links that is only available for certain users.

My problem is, I created a helper class called Navigation, but I am so certain that I MUST NOT hard-code the links in there, instead I want that helper class to just read an object sent from somewhere, and then will return the desired navigation array to a page.

Follow up questions, where do you think should i keep the links that will only be available for students, for admins. Should i just keep them in a text-file?

or if it is possible to create a controller that passes an array of links, for example a method in nav_controller class -> studentLinks(){} that will send an array of links to the helper class, the the helper class will then send it to the view..

Sorry if I'm quite crazy at explaining. Do you have any related resources?


Solution

  • From your description it seems that you are building some education-related system. It would make sense to create implementation in such way, that you can later expand the project. Seems reasonable to expect addition of "lectors" as a role later.

    Then again .. I am not sure how extensive your knowledge about MVC design pattern is.

    That said, in this situation I would consider two ways to solve this:

    1. View requests current user's status from model layer and, based on the response, requests additional data. Then view uses either admin or user templates and creates the response.

      You can either hardcode the specific navigation items in the templates, from which you build the response, or the lit of available navigation items can be a part of the additional information that you requested from model layer.

      The downside for this method is, that every time you need, when you need to add another group, you will have to rewrite some (if not all) view classes.

    2. Wrap the structures from model layer in a containment object (the basis of implementation available in this post), which would let you restrict, what data is returned.

      When using this approach, the views aways request all the available information from model layer, but some of it will return null, in which case the template would not be applied. To implement this, the list of available navigation items would have to be provided by model layer.

    P.S. As you might have noticed from this description, view is not a template and model is not a class.