I'm trying to add a Dashboard to my ZF2 Application. I want to build up the Dashboard by collecting views from multiple modules. Every module I add to the Application needs to add it's own view to the Dashboard.
I'm not sure which would be the best approach. Here are my Ideas:
1. In the controller action which is rendering the Dashboard I trigger an Event. This event is catched by my modules, for example in their init() method. Every module add it's own ViewModel. In my controller action I could add them to the base ViewModel. Problems: No possibility for setting sort orders for the Dashboard partials (the ViewModels added by the other modules)
2. I could extend the ZF2 Navigation to collect more information than labels and routes. It needs to collect headlines and factory class names that can create widgets and then renders them like native navigation renders the menu items. I could add parts to the Dashboard from module configs then. I'm not sure if this is possible but I will need to digg into the ZF2 Navigation to find out then. I don't really think it's possible to create a complex Dashboard like that.
What do I need in my Dashboard? For example I want to list products having a critical wearhouse stock.
How would you guys build up such a Dashboard?
[edit - usage]
Hey @Rudolph Gottesheim, I'm going to explain more in detail how it works when I find some more time. In order to give you a quick answer I'm going to explain how I can work now with the Dashboard.
I can configure multiple Dashboards now in my module.config.php and it looks like this
'application_dashboard' => array(
'application' => array(
'elements' => array (
array(
'label'=>'some dashboard element',
'sortorder'=>2,
//'contentString'=>'instead of contentService or contentElement'
//'contentElement'=> new SomeDashboardElementContent(),
'contentService'=>'name_of_a_service_that_provides_an_element',
'viewHelper'=>'name_of_a_viewhelper'
),
),
),
'admin' => array(
'elements' => array (
array(
'label'=>'label for admin dashboard element',
'sortorder'=>2,
//'contentString'=>'hello world'
),
),
),
),
So I can add new dashboards and new elements from each module. The elements can become very complex so for example a dashboard element could present a grid of users from the database. That's why I ended up with the 'contentService' in the elements configuration.
This service does all the complex stuff and has access to other services so it could build up anything and returns the element. My DashboardProviderService will use the ServiceLocator to call the configured 'contentService' in order to get the element. The value of 'viewHelper' is a ViewHelper that knows how to render that specific Element. As I said, the element could contain a grid or even a ViewModel. My DashboardProviderService uses the ViewHelperManager Service to retrieve that specific ViewHelper from that configuration.
I also created a ViewModelRenderer view helper which has access to the ViewRenderer Service of ZF2 so the Elements can also contain instances of ViewModel.
It's also possible to add a 'contentElement' directly when it is not a dynamic one or just add a string which is going to be rendered inside the dashboard element.
Here is the current list of my classes which I'm going to post and explain in more detail later:
Application\Service\DashboardProviderService
Application\Dashboard\Container implements Countable, RecursiveIterator
Application\Dashboard\Dashboard extends Application\Dashboard\Container
Application\Dashboard\Element\Element
Application\Dashboard\View\Helper
[edit - classes]
... coming soon ...
[edit - next steps]
- I'm going to add a 'route' value for dashboard elements that can be used to render a button or a link on the whole dashboard element
- I'm going to integrate my access control for the dashboard in order to filter the dashboard by role permissions of the user. I'm using ZfcRbac and the route Firewall so at the moment I'm thinking of filtering access to dashboard elements by the configured route value of the elemens.