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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探