Best way to allow packages to register widgets, etc. with the host app?
My app allows users to submit various types of content. This content is held for admin review before it's published on the site. Each type of content (links, photos, etc.) is managed by a separate package.
On my admin dashboard (which is part of the main app), I'd like to display a widget for each package, with a message such as "18 Links Pending Review", and a link to a page which will allow the admin to review those submissions.
I could just hardcode this in my DashboardController, but that's sloppy. How can I set up a system so that my packages can make the dashboard aware that they have dashboard widgets, and then provide the dashboard with the views for those widgets when needed? Something like:
class MyPackageClass implements WidgetPublisherInterface {
public function getSupportedWidgets()
{
return ['links'];
}
public function getLinksWidget()
{
// somehow return the view for the Links widget
}
}
And then, in my DashboardController:
public function index()
{
// this is just pseudocode
foreach ($packages as $package) {
$package_widgets = $package->getSupportedWidgets();
foreach ($package_widgets as $package_widget) {
// do something
}
}
}
This mechanism would also be useful for registering nav items, etc.
There are a few problems I would need to solve:
-
On what class should I put the getPackageWidgets() and getLinksWidget() methods? It would need to be a class that DashboardController can easily infer and access.
-
How can my DashboardController get a list of all installed packages?
-
What's the best way for getPackageWidgets() to return a view to DashboardController?
Is this a job for providers? My experience with providers in other frameworks suggests so, but I find that "provider" can mean different things in different ecosystems, and Laravel's service providers and service container still confuse the shit out of me.
Please or to participate in this conversation.