doujiao2014 2018-07-13 06:47
浏览 121
已采纳

如何让控制器在Symfony 4中返回响应?

I installed the DataTableBundle in Symfony 4 according to this tutorial: https://omines.github.io/datatables-bundle/#quickstart

 <?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Omines\DataTablesBundle\Adapter\ArrayAdapter;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\Controller\DataTablesTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class DataTableController extends Controller
{

  /**
  * @Route("/")
  */

  use DataTablesTrait;

  public function showAction(Request $request)
  {
    $table = $this->createDataTable()
    ->add('firstName', TextColumn::class)
    ->add('lastName', TextColumn::class)
    ->createAdapter(ArrayAdapter::class, [
      ['firstName' => 'Donald', 'lastName' => 'Trump'],
      ['firstName' => 'Barack', 'lastName' => 'Obama'],
    ])
    ->handleRequest($request);

    if ($table->isCallback()) {
      return $table->getResponse();
    }

    $this->render('list.html.twig', ['datatable' => $table]);
  }
}

But I get an error message by Symfony:

Uncaught PHP Exception LogicException: "The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?" at /Users/work/project/vendor/symfony/http-kernel/HttpKernel.php line 165

I tried to change this line:

$this->render('list.html.twig', ['datatable' => $table]);

into this:

return $this->render('list.html.twig', ['datatable' => $table]);

but then I get a blank page with the text Loading... and nothing else.

list.html.twig:

<!-- in the <head> section -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.2.1/dt-1.10.16/datatables.min.css"/>


<!-- Insert this where you want the table to appear -->
<div id="presidents">Loading...</div>

<!-- before the closing <body> tag -->
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.2.1/dt-1.10.16/datatables.min.js"></script>


<!-- Insert this at the end of your body element, but before the closing tag -->
<script src="{{ asset('bundles/datatables/js/datatables.js') }}"></script>
<script>
$(function() {
    $('#presidents').initDataTables({{ datatable_settings(datatable) }});
});
</script>

Services.yaml:

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: 'en'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.


    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
  • 写回答

1条回答 默认 最新

  • douguazhi5966 2018-07-13 06:57
    关注

    The missing return was indeed the problem. The reason for having a blank page with the word "Loading..." on it will be located in your list.html.twig. But for solving this problem you have to provide more code.

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

报告相同问题?

悬赏问题

  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿