mojolicious - Delegate Rerouting to Controller -
mojolicious + hypnotoad.
i want controllers reroute remaining portion of request, don't have declare routes @ level of main script.
so example, '/foo/bar/baz' should route controller 'foo', decide routing 'bar/baz', internal itself.
main script :
package myapp; use mojo::base 'mojolicious'; use mojolicious::plugin::config; sub startup { $self = shift; $self->moniker('myapp'); $self->plugin('config'); $r = $self->routes; $r->any('/foo/*remainder')->to('foo#rerouter')->name('test_name'); }
i've tried 1 way, dynamically adding routes, running many times shows inconsistency - caused race condition or :
package myapp::controller::foo; use mojo::base 'mojolicious::controller'; sub rerouter { $self = shift; $r = mojolicious::routes->new; $r->get('bar/:baz')->to('myinternalcontrolleraction'); $current_route = $self->current_route; # 'test_name' $self->app->routes->find($current_route)->add_child($r); } sub myinternalcontrolleraction { #stuff }
that seems consistent other answer :
"the router dynamic until first request has been served, after that, router cannot change routes"
https://stackoverflow.com/a/22291864/2304437
even if did work though, execution exit controller foo before re-entering on different action via dynamically added route(s).
the other way can create dispatch table in "re-router" of controller, since have access remainder of request anyway :
sub rerouter { $self = shift; $remainder = $self->stash->{remainder}; # ... }
but lose benefits of mojolicious' dispatcher - have parse remaining request url / path myself. feels dirty. hooks don't seem appropriate here either.
how can elegantly let controller route own child portion ?
Comments
Post a Comment