如何在sonata管理包中使用CRUD控制器创建自定义表单?

时间:2017-11-10 13:38:12

标签: symfony sonata-admin symfony-sonata

我是sonata管理员的初学者,我需要知道如何使用路由和控制器以及index.html.twig创建自定义表单。

   app.admin.category:
        class: AppBundle\Admin\CategoryAdmin
        arguments: [~, AppBundle\Entity\Category, SonataAdminBundle:CRUD]
        tags:
            - { name: sonata.admin, manager_type: orm, group: adminn, label: Category }
        public: true

2 个答案:

答案 0 :(得分:1)

您需要创建CRUD控制器。对于每个管理服务,您都有默认管理服务,您可以覆盖它。 在您的情况下,服务是app.admin.category

如何创建CRUD控制器?我将以最简单的方式解释。

  1. AppBundle/Controller/Admin/文件夹中创建类,并将其命名为CategoryAdminController。 这是你班级的代码:

    <?php
    
    namespace AppBundle\Controller\Admin;
    
    use Sonata\AdminBundle\Controller\CRUDController;
    
    /**
     * Class CategoryAdminController
     * @package AppBundle\Controller\Admin\
     */
    class CategoryAdminController extends CRUDController
    {
    }
    

    正如您所看到的,此类扩展了默认的SonataAdminCRUDController。

  2. 然后指向你的奏鸣曲服务,它必须使用这个类,因为它是CRUDContoller。 在service.yml中更改标准CRUD控制器:

    app.admin.category:     class:AppBundle \ Admin \ CategoryAdmin     参数:[〜,AppBundle \ Entity \ Category,AppBundle:Admin \ CategoryAdmin]     标签:          - {name:sonata.admin,manager_type:orm,group:adminn,label:Category}     public:true

  3. 现在您已准备好在新的Controller中进行新操作。我们称之为changeAction。

     /**
      * @param int $id
      *
      * @return Response
      */
     public function changeAction($id = null)
     {
         try {
            if ($id !== null) {
                $category = $this->admin->getObject($id);
            }
        } catch (NotFoundHttpException $e) {
            error_log($e->getMessage());
        }
    
        // .. Here is a code of creating your custom form with variable $form
    
    
        return $this->render('@App/Admin/CRUD/index.html.twig', [
            'custom_form' => $form->createView(),
        ]);
     }
    
  4. 为您的新动作制作路线!在CategoryAdmin服务覆盖configureRoutes()方法

    /**
     * @param RouteCollection $collection
     */
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->add('change', $this->getRouterIdParameter().'/change');
    }
    
  5. 最后 - 我们可以按下按钮:

    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            //.. Your fields...
            ->add('_action', 'actions', [
                'label' => ' ',
                'actions' => [
                    //'show' => array(),    // I removed show action button, but you can uncomment
                    //'edit' => array(),    // I removed edit action button, but you can uncomment
                    //'delete' => array(),  // I removed delete action button, but you can uncomment
                'change' => [
                        'template' => 'AppBundle::Admin\CRUD\Buttons\change_category_button.html.twig',
                    ]
                ]
            ])
        ;
    }
    
  6. 当然不要忘记为新按钮创建一个树枝模板并将其放在正确的位置AppBundle/Resources/views/Admin/CRUD/Buttons/

    //'AppBundle::Admin\CRUD\Buttons\change_category_button.html.twig':
    
    {% if admin.isGranted('EDIT', object) and admin.hasRoute('change') %}
        <a href="{{ admin.generateObjectUrl('change', object) }}" class="btn btn-sm btn-primary edit_link" title="Your title here"  style="display: inline-block">
            <i class="fa fa-toggle-on"> </i>
            Change Button
        </a>
    {% endif %}
    

答案 1 :(得分:0)

对于最新的symfony / sonata,

staskrak解决方案不再对我有用。所以我采用了https://sonata-project.org/bundles/admin/3-x/doc/cookbook/recipe_custom_view.html

的解决方案

只需添加文件admin / my_form.html.twig之类的

{# templates/admin/my_form.html.twig #}

{% extends '@SonataAdmin/standard_layout.html.twig' %}

{% block sonata_admin_content %}
    {{ form(form) }}
{% endblock %}

并在SomeAdminController中使用它

public function batchActionMyAction(ProxyQueryInterface $selectedModelQuery, Request $request = null)
{
    $this->admin->checkAccess('edit');
    $this->admin->checkAccess('delete');

    $form = $this->createFormBuilder([])
        ->add('smsText', TextType::class)
        ->add('save', SubmitType::class, ['label' => 'Create Task'])
        ->getForm();

    return $this->render('Admin/my_form.html.twig', [
        'form' => $form->createView(),
    ]);
}