I have example custom form type:
我有例子自定义表单类型:
namespace Acme\SimpleBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ExampleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setAction(****-----GENERATE-ROUTE-ABSOLUTE-URL-HERE-----****)
->add('email', 'text')
->add('send', 'submit');
}
public function getName()
{
return 'example';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'error_bubbling' => true
));
}
}
How can I generate absolute URL (like http://example.com/admin/check_form) to my route?
如何生成到路由的绝对URL(如http://example.com/admin/check_form) ?
$this->generateUrl()
doesn't work, becuse it's not a controller, and $this->get('router')->generate()
also doesn't work, I don't know why.
$this->generateUrl()不起作用,因为它不是控制器,$this->get('router')->generate()也不起作用,我不知道为什么。
2 个解决方案
#1
12
There are two ways to do it:
有两种方法:
- Register your form type as service and inject the router in it: doc
- 将表单类型注册为服务并将路由器注入其中:doc
-
Pass your action as option when you create your form in your controller:
在控制器中创建表单时,将操作作为选项传递:
$form = $this->createForm(new FormType(), null, array('action' => $this->generateUrl('your_route', array(/* your route parameters */), true));
Note that you must pass
true
as last argument in order to force the router to generate an absolute url (as you asked for for Symfony < 2.8). If you're using Symfony3, useUrlGeneratorInterface::ABSOLUTE_URL
instead of true as suggested by Emilie in the comments.请注意,为了强制路由器生成一个绝对url(如您所要求的Symfony < 2.8),您必须将true作为最后一个参数传递。如果使用Symfony3,请使用UrlGeneratorInterface: ABSOLUTE_URL,而不是Emilie在评论中建议的true。
#2
4
You have to declare your FormType as a service, and with the service container add the router (service id: router), then in your FormType :
您必须将表单类型声明为服务,然后使用服务容器添加路由器(服务id: router),然后使用您的表单类型:
$this->router->generate('your_route_name', [] /* params */, true /* absolute */);
Here the doc : http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
这里是doc: http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
#1
12
There are two ways to do it:
有两种方法:
- Register your form type as service and inject the router in it: doc
- 将表单类型注册为服务并将路由器注入其中:doc
-
Pass your action as option when you create your form in your controller:
在控制器中创建表单时,将操作作为选项传递:
$form = $this->createForm(new FormType(), null, array('action' => $this->generateUrl('your_route', array(/* your route parameters */), true));
Note that you must pass
true
as last argument in order to force the router to generate an absolute url (as you asked for for Symfony < 2.8). If you're using Symfony3, useUrlGeneratorInterface::ABSOLUTE_URL
instead of true as suggested by Emilie in the comments.请注意,为了强制路由器生成一个绝对url(如您所要求的Symfony < 2.8),您必须将true作为最后一个参数传递。如果使用Symfony3,请使用UrlGeneratorInterface: ABSOLUTE_URL,而不是Emilie在评论中建议的true。
#2
4
You have to declare your FormType as a service, and with the service container add the router (service id: router), then in your FormType :
您必须将表单类型声明为服务,然后使用服务容器添加路由器(服务id: router),然后使用您的表单类型:
$this->router->generate('your_route_name', [] /* params */, true /* absolute */);
Here the doc : http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
这里是doc: http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html