doupaxia2478
2016-10-06 10:07
采纳率: 100%
浏览 36
已采纳

在Symfony的配置中使用route作为url

I use CKEditor and want to use uploadimage plugin. I need to specify uploadUrl in config.yml. How can I put here a route instead of direct url?

ivory_ck_editor:
    default_config: my_config
    configs:
        my_config:
            extraPlugins: "lineutils,widget,notificationaggregator,uploadwidget,notification,uploadimage,wordcount"
            uploadUrl: '/admin/upload'

I know I can redefine config with form builder

$builder->add('field', 'ckeditor', array(
    'config' => array('uploadUrl' => ...),
));

But I want to do it once for every form. Which is the best way?

图片转代码服务由CSDN问答提供 功能建议

我使用CKEditor并想使用 uploadimage 插件。 我需要在config.yml中指定 uploadUrl 。 如何在这里放置路由而不是直接url?

  ivory_ck_editor:
 default_config:my_config 
 configs:
 my_config:
 extraPlugins:“lineutils,widget,  notificationaggregator,uploadwidget,notification,uploadimage,wordcount“
 uploadUrl:'/ admin / upload'
   
 
 

我知道我可以使用表单构建器重新定义配置

  $ builder-> add('field','ckeditor',array(
'config'=> array('uploadUrl'=> ...),
  )); 
   
 
 

但我想为每个表单做一次。 哪种方法最好?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dragon321723 2016-10-06 16:33
    已采纳

    If you define your form as a service, you could inject the router and use it to generate the path in your form. (This ignores the possibility of setting it in config.yml.)

    services:
        app.form.type.yourformtype:
            class: AppBundle\Form\YourFormType
            arguments: [@router]
            tags:
                - { name: form.type }
    

    Then, in your form:

    <?php
    
    namespace AppBundle\Form
    
    use Symfony\Bundle\FrameworkBundle\Routing\Router;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\AbstractType;
    
    class YourFormType extends AbstractType
    {
        private $router;
    
        public __construct(Router $router)
        {
            $this->router = $router;
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $uploadRouteName = 'app_admin_upload'; // Or whatever maps out to /app/admin, re: your original question
    
            ...
    
            $builder->add('field', 'ckeditor', array(
                'config' => array('uploadUrl' => $this->router->generate($uploadRouteName)),
            ));
    
            ...
    
        }
    }
    

    To truly do this once for every form you should consider extending the ckeditor formtype and adding your route to uploadUrl in the configureOptions method using the OptionsResolver.. Then update the service definition to inject the router to that class, and in place of ckeditor in the second argument to add methods, use YourCkeditorExtendedType::class, and you won't need to add config each time.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题