LogicException : The controller must return a response (success ! given) [duplicate]
-
25-02-2021 - |
Question
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 !';
}
Solution
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.