【发布时间】:2018-07-13 06:47:11
【问题描述】:
我根据本教程在 Symfony 4 中安装了 DataTableBundle: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]);
}
}
但我收到 Symfony 的错误消息:
未捕获的 PHP 异常 LogicException:“控制器必须返回一个 响应(给定空值)。您是否忘记添加退货声明 在你的控制器的某个地方?”在 /Users/work/project/vendor/symfony/http-kernel/HttpKernel.php 第 165 行
我试图改变这一行:
$this->render('list.html.twig', ['datatable' => $table]);
进入这个:
return $this->render('list.html.twig', ['datatable' => $table]);
然后我得到一个空白页,上面写着Loading...,没有别的。
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
【问题讨论】:
-
您是否像这里提到的那样将配置部分添加到您的 config.yml 中? omines.github.io/datatables-bundle/#configuration。因为被寻址的表有一个默认模板,这将导致将html代码注入到list.html.twig中的占位符#presidents
-
@KlausSchürg 我将它添加到我的 service.yaml 中,但这不起作用,所以我删除了它。我想我没有 config.yaml
-
如果我将它添加到 services.yaml 我得到错误
The configuration key "language_from_cdn" is unsupported for definition "datatables" in "/Users/work/project/config/services.yaml". Allowed configuration keys are "alias", "parent", "class", "shared", "synthetic", "lazy", "public", "abstract", "deprecated", "factory", "file", "arguments", "properties", "configurator", "calls", "tags", "decorates", "decoration_inner_name", "decoration_priority", "autowire", "autoconfigure", "bind" in /Users/work/project/config/services.yaml (which is loaded in resource "/Users/work/project/config/services.yaml"). -
在打开呈现的 html 的源代码时,您是否安装了资产并且它们是否可用?可能只是js没有加载?
-
很高兴听到这个消息 :-) 周末愉快...
标签: php symfony return-value