Search code examples
perlmojolicious

How to map nested routes to sub-classes in Mojolicious


I am using Mojolicious and will be building services at:

/admin/users/view/1
/admin/posts/view/1 
/admin/comments/view/1

How do I create the routes so that the controller matches 'admin/users' and translates that to Admin::Users

I want to achieve:

my $r = $self->routes;
$r->get('/*controller/:action/:id');

There doesn't seem to be a good way to do this without having to write a route for every admin controller, which seems like a waste. The above statement yields a controller of 'Admin/posts' instead of 'Admin::Posts'

Can anyone shed some light on how to do this?


Solution

  • OK I found it.

    $r->get('/admin/:controller/:action/:id')->to(namespace=>'MyApp::Admin);