php - 如何防止Symfony 4中出现ParseError?

标签 php symfony error-handling syntax controller

src/Controller/DataTableController.php

<?php

use DataTables\DataTablesInterface;

/**
 * Symfony 3.4 and above
 *
 * @Route("/users", name="users")
 *
 * @param Request $request
 * @param DataTablesInterface $datatables
 * @return JsonResponse
 */
public function usersAction(Request $request, DataTablesInterface $datatables): JsonResponse
{
    try {
        // Tell the DataTables service to process the request,
        // specifying ID of the required handler.
        $results = $datatables->handle($request, 'users');

        return $this->json($results);
    }
    catch (HttpException $e) {
        // In fact the line below returns 400 HTTP status code.
        // The message contains the error description.
        return $this->json($e->getMessage(), $e->getStatusCode());
    }
}

/**
 * Symfony 3.3 and below
 *
 * @Route("/users", name="users")
 *
 * @param Request $request
 * @return JsonResponse
 */
public function usersAction(Request $request): JsonResponse
{
    try {
        /** @var \DataTables\DataTablesInterface $datatables */
        $datatables = $this->get('datatables');

        // Tell the DataTables service to process the request,
        // specifying ID of the required handler.
        $results = $datatables->handle($request, 'users');

        return $this->json($results);
    }
    catch (HttpException $e) {
        // In fact the line below returns 400 HTTP status code.
        // The message contains the error description.
        return $this->json($e->getMessage(), $e->getStatusCode());
    }
}

我收到错误消息:

enter image description here

最佳答案

类的结构(与Symfony不相关)是

class MyClassController() {

  public function myMethodAction() {
  }
}

无法避免解析错误,因为这只是PHP的无效语法。

关于php - 如何防止Symfony 4中出现ParseError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51187450/

相关文章:

php - 将 Woocommerce 购物车项目自定义数据保存为订单项目元数据,将其显示在订单和电子邮件上

php - 显示过去几天的结果

php - 在输入字段中创建一个跨度,例如 Facebook 消息

php - sonata admin + a2lix + gedmo 可翻译问题

php - 如何比较 Symfony 3 中的密码(Bcrypt 哈希)?

php - 将 PHP 应用程序与 MySQL Fabric 连接(使用 php 感知连接器)

php - 服务器 :run Exception There are no commands defined in the "server" namespace

error-handling - sass/gulp-sass对某些错误(无效属性)保持沉默

javascript - 如何处理简单对等库中 Uncaught Error ?

bash - 我的bash脚本崩溃了吗?