1

I have a very simple bundle controller

<?php

namespace Mnab\ContactBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction($anything)
    {
        return new \Symfony\Component\HttpFoundation\Response(__CLASS__ .' '.$anything);
        //return $this->render('MnabContactBundle:Default:index.html.twig', array('name' => $anything));
    }
}

I have roter config in YML files

CASE 1: My routing for this controller looks like:

mnab_contact_homepage:
    pattern:  contact/{anything}
    defaults: { _controller: MnabContactBundle:Default:index, anything: null }
    requirements:
      anything: .*

In this case application accepts for this controller the following urls:

/contact /contact/ /contact/bla /contact/bla/bla/blabla

and so on

CASE 2 My routing for this controller looks like:

mnab_contact_homepage:
    pattern:  contact/
    defaults: { _controller: MnabContactBundle:Default:index, anything: null }
    requirements:
      anything: .*

In this case application accepts only

/contact/

and

if there is url:

/contact -> it makes redirection (dont' know what type 301 or 302) to /contact/ url

CASE 3 My routing for this controller looks like:

mnab_contact_homepage:
    pattern:  contact
    defaults: { _controller: MnabContactBundle:Default:index, anything: null }
    requirements:
      anything: .*

In this case application accepts only

/contact

Expected results

In case 3 everything is as I expect but case 1 and 2 works not as I expect

Questions

  1. Why in case 1 application accepts also /contact url ? I have clearly expect /contact/ url not contact/

  2. Why in case 2 applications accepts also /contact url making redirection for it to /contact/

  3. Why behaviour is different in case 1 and case 2

  4. Is it possible to change this behaviour - for example not accept at all /contact in my case ? (In fact I don't mind because I want some urls with trailing slash but in case 1 I'll have to do redirection for version without trailing slash)

  5. Does Symfony do such "tricks" for all urls (also with extension as for example I define it works for /test.html and Symfony makes it work also for /test.html/ ) or only for those without extension?

  6. Where can I read a bit more about such cases?

4

1 回答 1

2

为什么如果 1 应用程序也接受 /contact url?我显然期望 /contact/ url 不是 contact/

/contact意味着您将省略anything在这种情况下的参数null

/contact/表示您没有遗漏anything参数。在这种情况下NotFoundHttpException应该抛出,因为您没有为anything参数提供任何值。

为什么如果 2 应用程序也接受 /contact url 将其重定向到 /contact/

这是Symfony2 路由组件的行为,无法真正了解更多。

为什么案例 1 和案例 2 的行为不同

因为在案例 1 中,路由中有一个参数。该参数虽然有一个默认值。在情况 2 中,路由中没有参数。

是否有可能改变这种行为 - 例如在我的情况下根本不接受 /contact

是的,例如使anything参数成为必需的,而不是可选的。

Symfony 是否为所有 url 做这样的“技巧”(也有扩展名,例如我定义它适用于 /test.html 和 Symfony 使它也适用于 /test.html/ )或只适用于那些没有扩展名的

路由组件的行为是一致的,不管你使用testor test.html

我在哪里可以阅读更多关于此类案例的信息?

这方面的资源不会很多,但是您可以随时查看路由组件的源代码。

但是,可以在此处找到路由组件的文档Routing

于 2014-05-05T21:00:26.440 回答