如何在symfony2中路由?

时间:2012-10-10 14:39:44

标签: php symfony routing

好吧,在我的routing_dev.yml

中有以下内容
_welcome:
    pattern:  /
    defaults: { _controller: AcmeDemoBundle:Welcome:index }

homepage:
    pattern:   /
    defaults: { _controller: AcmeDemoBundle:Ideup:index }

aboutme:
    pattern:   /aboutme
    defaults: { _controller: AcmeDemoBundle:Ideup:about }

_ideup:
    resource: "@AcmeDemoBundle/Controller/IdeupController.php"
    type:     annotation
    prefix:   /ideup

_form:
    resource: "@AcmeDemoBundle/Controller/FormController.php"
    type:     annotation

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_main:
    resource: routing.yml

我想要一个简单的表格:

<form action="{{ path('_form') }}" method="POST" id="contact_form">

但我继续得到以下异常:"Route "_form" does not exist."

我只是不知道在哪里看,做什么,有什么想法?

1 个答案:

答案 0 :(得分:4)

_form:
  resource: "@AcmeDemoBundle/Controller/FormController.php"
  type:     annotation

这只会从FormController加载带注释的路由。

例如:

/**
 * @Route("/my/route", name="my_route")
 */
public function myRouteAction() { ... }

如果你想要一个_form路由,你应该从FormController加载所有注释路由,或者以另一种方式定义它:

_form:
  pattern: /path/to/your/form
  defaults: { _controller: AcmeDemoBundle:Form:myFormAction }

当然myFormAction()中应该存在FormController