0

i have a service that return for me true in the node (article) successfully create and false if something wrong , but in the controller i am getting this error 'LogicException : The controller must return a response (success ! given). dans Symfony\Component\HttpKernel\HttpKernel->handleRaw() (ligne 169 de /var/www/html/vendor/symfony/http-kernel/HttpKernel.php).'

can anyone tell me where is the problem in the controller return of my code please ?

service

 public function createArticle()
  {
    $node = Node::create(array(
      'type' => 'article custom',
      'title' => 'article custom',
      'langcode' => 'fr',
      'uid' => '1',
      'status' => 1,
      'field_fields' => array(),
    ));
    $node->save();
    if($node)
      return true;
    else
      return false;
  }

the controller methode

 public function myPageCreate()
  {
    $service = \Drupal::service('mymodule.ran');
    $createnode = $service->createArticle();
    if($createnode)
      return 'success !';
    else
      return 'somthing wrong !';
  }
2
  • You need to return a render array or a Response object from a controller. You're returning a string which isn't allowed.
    – Jaypan
    Jun 29, 2020 at 14:50
  • thank you guys , it's work for me Jun 29, 2020 at 15:21

1 Answer 1

0

A controller must provide a response, but drupal has extended the response handling to look for render arrays, that are then used the generate a response with the rendered html. If you don't provide a render array, then there is no response and then this error message will come up.

Not the answer you're looking for? Browse other questions tagged or ask your own question.