Symfony Formbuilder 获取路径信息

Symfony Formbuilder get pathInfo

有没有办法在 buildForm 函数中 return 页面的 pathInfo 或 URL?

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

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

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

您的控制器操作

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

您的表单类型class

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);
    }
}