6
我想在我的Mojolicious應用中實施類似「動態」路線的內容。我有一些預定義的「靜態」路由和一個具有URL別名的數據庫表:'/ alias' - >'/ URL'。現在我正在定義路線,它看起來像這樣:Mojolicious中的「動態」路線
before_dispatch => sub {
my ($self, $controller) = @_;
my $path = $controller->tx->req->url->path->to_string;
if (my $alias = $controller->app->model->alias->find({ alias => $path })) {
my $match = Mojolicious::Routes::Match->new(get => $alias->{uri});
my $routes = $controller->app->routes;
$match->match($routes);
$routes->route($path)->to($match->captures);
}
但是有沒有更好的方法?
你並不需要檢查,如果如果你把包羅萬象在你的路由列表的底部存在的路線。路線按順序嘗試並使用第一個匹配項。 – 2017-08-10 09:18:36