Silex 中的安装操作 - 额外的尾部斜线
Mount operation in Silex - additional trailing slash
我有一个用 Silex Framework 开发的小型后端。
我尝试让这个请求在 POST:
上工作
但是当使用挂载操作时只有这个请求有效:
如您所见,我必须向请求添加一个额外的尾部斜杠。
下面是一些无法按预期工作的代码:
//index.php
$app->mount("/other", new FeedbackOther());
//FeedbackOther.php
$feedbackOther->get("/", "FeedbackOtherController::index")->bind('other');
$feedbackOther->post("","FeedbackOtherController::store");
如果我做类似
//index.php
$app->post('/other', "FeedbackOtherController::store");
$app->mount("/other", new FeedbackOther());
//FeedbackOther.php
$feedbackOther->get("/", "FeedbackOtherController::index")->bind('other');
POST 请求在没有附加斜线的情况下工作,但在这种情况下,我看不到使用挂载操作的意义。
我也尝试过使用 .htacces 重写,但重写规则会转换 GET 中的 POST 请求。
安装路由集合和简单地将路由限制为 post
是有区别的。添加 mount
意味着在特定命名空间下创建路由集合,在您的情况下是 /other
。这意味着此命名空间的默认 URL 将是 /other/
- 尾部斜线也是必需的。当您将路由定义为特定方法时,这不适用,例如 post
。简而言之 - 这也是 Silex
和 Symfony
中的预期行为。前段时间github对此进行了激烈的讨论,所以让我copy/paste你Fabien留下的笔记(作者):
Let me explain the current behavior of mounted routes so that everybody understand the behavior explained by several people in this thread (and this behavior is the same as the one in Symfony, so it applies to Symfony as well).
The simplest URL possible is / (an empty URL does not mean anything and is silently converted by browsers to /, so when you are requesting http://google.com, the actual request is for http://google.com/).
When you mount a route collection under a prefix, Silex/Symfony creates a new "namespace" for those URLs. When you define / in a route collection and then mount it under /foo, the URL to access this route is /foo/. Requesting /foo in this case does not make sense.
So, as of today, it is not possible to define a callback for /foo when mounting a route collection under /foo. I can see the limitation of the current behavior but I can't think of a possible solution that does not feel hackish.
如果您对整个故事感兴趣 - this 是引用的地方。
我有一个用 Silex Framework 开发的小型后端。 我尝试让这个请求在 POST:
上工作但是当使用挂载操作时只有这个请求有效:
如您所见,我必须向请求添加一个额外的尾部斜杠。
下面是一些无法按预期工作的代码:
//index.php
$app->mount("/other", new FeedbackOther());
//FeedbackOther.php
$feedbackOther->get("/", "FeedbackOtherController::index")->bind('other');
$feedbackOther->post("","FeedbackOtherController::store");
如果我做类似
//index.php
$app->post('/other', "FeedbackOtherController::store");
$app->mount("/other", new FeedbackOther());
//FeedbackOther.php
$feedbackOther->get("/", "FeedbackOtherController::index")->bind('other');
POST 请求在没有附加斜线的情况下工作,但在这种情况下,我看不到使用挂载操作的意义。
我也尝试过使用 .htacces 重写,但重写规则会转换 GET 中的 POST 请求。
安装路由集合和简单地将路由限制为 post
是有区别的。添加 mount
意味着在特定命名空间下创建路由集合,在您的情况下是 /other
。这意味着此命名空间的默认 URL 将是 /other/
- 尾部斜线也是必需的。当您将路由定义为特定方法时,这不适用,例如 post
。简而言之 - 这也是 Silex
和 Symfony
中的预期行为。前段时间github对此进行了激烈的讨论,所以让我copy/paste你Fabien留下的笔记(作者):
Let me explain the current behavior of mounted routes so that everybody understand the behavior explained by several people in this thread (and this behavior is the same as the one in Symfony, so it applies to Symfony as well).
The simplest URL possible is / (an empty URL does not mean anything and is silently converted by browsers to /, so when you are requesting http://google.com, the actual request is for http://google.com/).
When you mount a route collection under a prefix, Silex/Symfony creates a new "namespace" for those URLs. When you define / in a route collection and then mount it under /foo, the URL to access this route is /foo/. Requesting /foo in this case does not make sense.
So, as of today, it is not possible to define a callback for /foo when mounting a route collection under /foo. I can see the limitation of the current behavior but I can't think of a possible solution that does not feel hackish.
如果您对整个故事感兴趣 - this 是引用的地方。