1

コントローラーでテンプレートをレンダリングしようとしていますが、機能しません。次のエラーが表示されます。

LogicException: コントローラーは応答を返す必要があります (

こんにちはボブ!

与えられた)。Symfony\Component\HttpKernel\HttpKernel->handleRaw() 内 (core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php の 163 行目)。

私の機能:

public function helloAction($name) {
$twigFilePath = drupal_get_path('module', 'acme') . '/templates/hello.html.twig';
$template = $this->twig->loadTemplate($twigFilePath);
return $template->render(array('name' => $name));
}
4

3 に答える 3

3

Drupal 8 では、コントローラから Response オブジェクトまたはレンダリング配列を返します。したがって、次の 2 つのオプションがあります。

1) レンダリングされたテンプレートを Response オブジェクトに配置します。

public function helloAction($name) {
  $twigFilePath = drupal_get_path('module', 'acme') . '/templates/hello.html.twig';
  $template = $this->twig->loadTemplate($twigFilePath);
  $markup = $template->render(array('name' => $name));
  return new Response($markup);
}

2) レンダリングされたテンプレートをレンダリング配列に配置します。

public function helloAction($name) {
  $twigFilePath = drupal_get_path('module', 'acme') . '/templates/hello.html.twig';
  $template = $this->twig->loadTemplate($twigFilePath);
  $markup = $template->render(array('name' => $name));
  return array(
    '#markup' => $markup,
  );
}
于 2015-06-15T00:58:36.020 に答える
0
class ClientController extends ControllerBase implements ContainerInjectionInterface ,ContainerAwareInterface {

protected $twig ;

public function __construct(\Twig_Environment $twig)
{
    $this->twig = $twig ;
}


public function index()
{

    $twigFilePath = drupal_get_path('module', 'client') . '/templates/index.html.twig';
    $template = $this->twig->loadTemplate($twigFilePath);
    $user = ['user' => 'name'] ; // as example
    $markup = [
        '#markup' => $template->render( ['users' => $users ,'kit_form' => $output] ),
        '#attached' => ['library' => ['client/index.custom']] ,
    ];
    return $markup;

}

// this is called first then call constructor 
public static function create(ContainerInterface $container)
{
    return new static(
        $container->get('twig') ,
    );
}
}

コントローラーからの依存性注入によって小枝をレンダリングするこの完全な例

于 2015-12-07T14:32:44.277 に答える