Routing in dashboard widgets?

Is it possible to set up routing in a custom panel widget? We have a widget that clears the cache (quick way to clear it after updating backend stuff), but it’s currently dependent on routing that is set up in a plugin:

site/widgets/flusher/flusher.php

if(site()->user()->hasRole('admin')):
	return array(
  	'title' => 'Cache Flusher',
  	'html'  => function() {
    	// any data for the template
    	$data = array();
    	return tpl::load(__DIR__ . DS . 'template.php', $data);
  	}
	);
else:
	return false;
endif;

site/widgets/flusher/template.php

<form method="post" action="<?= url('cache-clear') ?>">
<input type="submit" name="cache-flusher" id="cache-flusher" class="btn btn-rounded btn-submit" value="Flush the cache" />
</form>

site/plugins/flusher/flusher.php

if(site()->user() && site()->user()->hasRole('admin')):
	kirby()->routes(array(
		array(
			'pattern' => 'cache-clear',
			'method' => 'POST',
			'action'  => function() {
				cache::flush();
				return header::redirect(url('/panel/'));
			}
		)
	));
endif;

That setup doesn’t work well for packaging as a submodule, but the routing doesn’t seem to work when moved to the widget file. Would there be another way to handle the route for that?

1 Like

Did you find a way yet? Need this to.

Not yet, and we’re not sure when we’d be able to get to it, but it looks like it may be possible using the new plugin registry system available in 2.3.0: https://getkirby.com/docs/developer-guide/plugins/registry