Home » Php » php – In PhpStorm warning method 'withJson' not found (Slim Framework)

php – In PhpStorm warning method 'withJson' not found (Slim Framework)

Posted by: admin July 12, 2020 Leave a comment

Questions:

In PhpStorm I get the warning message
“warning method ‘withJson’ not found” in \Psr\Http\Message\ResponseInterface” at te line:

return $response->withJson($toReturn, 200);

The code:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;


$app->get('/bedrijven', function (Request $request, Response $response) {
    require_once(CLASSES_PATH . "/class_bedrijven.php");
    $Bedrijven = new Bedrijven();

    $toReturn = $Bedrijven->get_bedrijven();
    return $response->withJson($toReturn, 200);
});

I already updated slim framework with composer to latest version 3.8.1 and added Slim as a plugin in PhpStorm.
The Vendor directorie is set to Sources and Excluded.

The only answer I can find is to turn off the warning messages in PhpStorm in Editor -> Inspections -> PHP -> Undefined -> undefined method.

Is there a better solution?

How to&Answers:

Method withJson is not defined in \Psr\Http\Message\ResponseInterface but in Slim\Http\Response (which implements the former), that means this method is something related to Slim framework. You can try this:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Slim\Http\Response as Response;