Symfony Formbuilder获取pathInfo

时间:2015-10-16 09:40:09

标签: symfony formbuilder

有没有办法在buildForm函数内返回页面的pathInfo或URL?

class RoomType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
     public function buildForm(FormBuilderInterface $builder, array $options)
{

 VarDumper::dump($builder->getRequestHandler());

1 个答案:

答案 0 :(得分:5)

仅当您拥有请求对象时。一种选择是在构造表单时传递请求。

您的控制器操作

$form = $this->createForm(new RoomType($request), new Room());

您的表单类型

class RoomType extends AbstractType
{
    protected $request;

    public function __construct(Request $request) {
        $this->request = $request;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        VarDumper::dump($this->request);
    }
}