简体   繁体   English

如何在Symfony 4中实现处理程序?

[英]How can I implement a handler in Symfony 4?

According to the Datatable Symfony bundle documentation, I need to implement a handler. 根据Datatable Symfony捆绑包文档,我需要实现一个处理程序。

You have to create a handler for each DataTable instance. 您必须为每个DataTable实例创建一个处理程序。 A handler must implements the DataTableHandlerInterface interface. 处理程序必须实现DataTableHandlerInterface接口。 The only function this interface contains is handle : 该接口包含的唯一功能是handle:

  /**
   * Handles specified DataTable request.
   *
   * @param DataTableQuery $request
   *
   * @throws DataTableException
   * @return DataTableResults
   */
  public function handle(DataTableQuery $request): DataTableResults;

What I need to know is, where exactly do I have to put this code? 我需要知道的是,我到底要将代码放在哪里? In what folder and on what page? 在哪个文件夹和哪个页面上?

You can put the handler file in src/DataTables/UsersDataTable.php for Symfony 4 projects. 您可以将处理程序文件放在Symfony 4项目的src/DataTables/UsersDataTable.php中。

In this file you will implement the request handler ( https://github.com/webinarium/DataTablesBundle/wiki#step-1-implement-request-handler ) Don't forget to register it as a service: https://github.com/webinarium/DataTablesBundle/wiki#step-2-register-the-handler 在此文件中,您将实现请求处理程序( https://github.com/webinarium/DataTablesBundle/wiki#step-1-implement-request-handler )不要忘记将其注册为服务: https:// github的.com / webinarium / DataTablesBundle /维基#步骤-2-寄存器的处理程序

UsersDataTable.php UsersDataTable.php

// src/DataTables/UsersDataTable.php
class UsersDataTable implements DataTableHandlerInterface
{
    ///
}

You register services in services.yaml 您在services.yaml中注册服务

#config/services.yaml

services:
   datatable.users:
       class: App\DataTables\UsersDataTable
       tags: [{ name: datatable, id: users }]
       arguments: [ '@doctrine' ]

After this you can invoke the handler from a controller action https://github.com/webinarium/DataTablesBundle/wiki#step-3-invoke-the-handler 之后,您可以从控制器操作https://github.com/webinarium/DataTablesBundle/wiki#step-3-invoke-the-handler调用处理程序

SomeController.php SomeController.php

// App\Controller\SomeController.php

public function usersAction(Request $request, DataTablesInterface $datatables): JsonResponse
{
   //
}

If you follow the instructions under: https://github.com/webinarium/DataTablesBundle/wiki#handlers-service-autoloading all your handlers will be autoloaded. 如果您遵循以下说明: https : //github.com/webinarium/DataTablesBundle/wiki#handlers-service-autoloading您的所有处理程序都将自动加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM