Silex的灵活动态路由 [英] Flexible dynamic routing with Silex

查看:62
本文介绍了Silex的灵活动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取请求是否可能具有未知数量的参数?

Is it possible to have an unknown number of arguments for a get request?

例如,这可行,但并不理想。

Example, this works but isn't ideal.

$app->get('/print/{template}/{arg1}/{arg2}', function ($template, $arg1, $arg2) use ($app)     {
  $str = $template . " " . $arg1 . " " . $arg2;
  return $str;
})
->value('template', FALSE)
->value('arg1', FALSE)
->value('arg2', FALSE);

我想要的是为模板变量后输入的任何内容返回的参数数组。我看不到需要超过4或5个参数,但我希望从一开始就尽可能灵活。

What I'd like is an array of arguments returned for anything entered after the template variable. I can't see more than 4 or 5 arguments being required, but i'd like this to be as flexible as possible from the start.

理想情况下,我想做这样的事情,

Ideally i'd like to do something like this,

$app->get('/pdf/{template}/{args[]}', function ($template, $args) use ($app) {
  $str = $template;;
  foreach($args as $arg)
    $str .= " " . $arg;

  return $str;
});


推荐答案

路由组件不支持此功能据我所知。

The routing component does not support this out of the box as far as I know.

我仔细研究了一下代码,得出的结论是添加这样一条路线将很困难。路由的工作方式是在匹配完成之前注册每个路由,因此该路由必须存在。这意味着不能有通配符路径。

I looked into the code a bit and came to the conclusion that adding such a route would be difficult. The way the routing works is that each route gets registered before the matching is done, so the route has to exist. This means there can not be a "wildcard route".

我不知道您是否考虑了这一点,但是您始终可以通过真实获取参数传递尽可能多的信息:

I don't know if you took this into account, but you can always pass as much information as you like through "real" get parameters:

/ print / template?opt​​ional1 = arg& optional2 = arg

另一种处理此问题的方法是注册一个before事件,自己查看请求并进行修改。例如,您可以将整个url分割为/,查看模式是否符合您期望的格式,然后将所有可选参数放入一个参数中,并在两者之间使用特殊字符分隔符。我不建议这样做,但有可能。

Another way you could handle this is by registering a before event, looking at the request yourself and modifying it. You could for example split the whole url by /, see if the pattern matches your expected format and then put all the optional arguments into one argument with a special character spacer in between. I would not suggest this, but it is possible.

另一种处理方法是注册您自己的ControllerCollection,获取实际请求并注册与实际匹配的路由如果有可选参数,则请求。我猜这会更干净。

Another way of handling this is by registering your own ControllerCollection, getting the actual request and registering a route which matches the actual request in case there are optional arguments. This would be a bit cleaner I guess.

解决此问题的一种方法可能是前端。如果您的请求总是将附加参数放入最后一个参数中,且中间使用特殊字符,则可以读取最后一个参数,将其拆分并使用该参数:

One way of solving this could be the front-end. If your request always puts additional parameters into the last parameter with a special character in between, you could read those last parameter, split it and work with that:

/ print / template / arg:arg

您可能会像这样:

$app->get('/print/{template}/{args}', function ($template, $args) use ($app)     {
  $args = explode(':', $args);
  $str = $template . " " . $args[0] . " " . $args[1];
  return $str;
})
->value('template', FALSE)
->value('args', FALSE);

我会选择想法1或4,主要是因为随着时间的流逝,2和3会非常混乱,看起来不是很干净。

I would go for idea 1 or 4, mainly because 2 and 3 will be very confusing over time and appear to be not very clean.

这篇关于Silex的灵活动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆