Corrected Response ----------------
I tried this on a Joomla 4 component which I have and it worked ok, so I reckon you should be able to get it working.
That exception gets created in the RouterFactory in libraries/src/Component/Router/RouterFactory.php. It's because it's trying to create the class name from:
$className = trim($this->namespace, '\\') . '\\' . ucfirst($application->getName()) . '\\Service\\Router';
On the admin site the $application->getName()
will return 'administrator', so it will build the namespace prefix using \Administrator, and will look for the Service Router class in administrator/components/<your component>/src/Service/Router.php
and thus not find it.
You could copy your Router.php to there, and change your namespace
statement in that PHP file to be in the \Administrator
side. Then it would find your class.
The alternative is to create the Router class yourself, rather than use the Joomla RouterFactory to create it. In your extension you can provide your own function
public function createRouter(CMSApplicationInterface $application, AbstractMenu $menu): RouterInterface
{
return new Router($application, $menu, $this->categoryFactory, $this->getDatabase());
}
where Router points to your site Router class. Then you would use the same Router class for both when you're on the front-end and the back-end.
You can see a bit more explanation at https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Upgrading_to_Joomla4#Router where the equivalent class is called TraditionalRouter, and there's a link to a zip file for that component.
If you're getting the menuitems inside your router code then to make it work on the back end you'll need to do
use Joomla\CMS\Factory;
$app = Factory::getApplication();
$sitemenu = $app->getMenu('site');
$sitemenu->load();
but you're probably aware of that if you have it working before.
Note also that there are some changes as to how you need to code your custom router when you upgrade to Joomla 4. That tutorial step lists those as well.