Smil4 JSON data

Hi All,

Request your help, I have recently updated our system from Slim3 to Slim4 and the below function is not working on Slim4 where as the same works perfectly in Slim3 , the return data is of type json(return $serverList;)

Slim3
$app->get(’/slist/{hName}’, function($request, $response, $args) use ($app) {
$ad = new Serverlist();
$hName = $args[‘hName’];
$serverList = $ad->getServerlist($hName);
return $serverList;
});

Slim4
$app->get(’/slist/{hName}’, function(Request $request, Response $response, array $args) use ($app) {
$ad = new Serverlist();
$hName = $args[‘hName’];
$serverList = json_encode($ad->getServerlist($hName), JSON_PRETTY_PRINT);
return $serverList;
});

Error: Invalid JSON data.

Hi All,

Was able to resolve this issue by modifying the code as below, and it is working as expected, but also throwing the below error in Apache log. hence request your help.

$app->get(’/slist/{hName}’, function(Request $request, Response $response, array $args) use ($app) {
$ad = new Serverlist();
$hName = $args[‘hName’];
$serverList = $ad->getServerlist($hName);
$sdata = json_encode($serverList);
$response->getBody()->write($sdata);
return $response;

Error:
Got error ‘PHP message: PHP Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in /techhub/web/includes/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php:93\nStack trace:\n#0 /techhub/web/includes/vendor/slim/slim/Slim/Routing/RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))\n#1 /techhub/web/includes/vendor/slim/twig-view/src/TwigMiddleware.php(125): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request))\n#2 /techhub/web/includes/vendor/slim/slim/Slim/MiddlewareDispatcher.php(140): Slim\Views\TwigMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))\n#3 /techhub/web/includes/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Psr7\Request))\n#4 /Playbooks/Automation/saptechhub/web/includes/vendor/slim/slim/Slim/App.php(211): Slim\MiddlewareDispatcher->handle(Objec…\n’, referer: http://techhub.com/static/css/dataTables/datatables.min.css

From,
Vino.B

Hi All,

Tried the below step’s , still no luck

created the file .htaccess above the public folder (web)
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

File .htaccess insied the public folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

index.php
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Slim\Exception\NotFoundException;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Selective\BasePath\BasePathDetector;

$container = new Container();
AppFactory::setContainer($container);

$container->set(‘view’, function() {
return Twig::create(’…/tpl’);
});

$app = AppFactory::create();
basePath = (new BasePathDetector(_SERVER))->getBasePath();
$app->setBasePath($basePath);
$app->add(TwigMiddleware::createFromContainer($app));

From,
Vino.B

You should add the Routing middleware before you add the BasePathMiddleware

Example: https://github.com/selective-php/basepath#slim-4-integration

Hi Odan,

  Thank you very much, I tried the solution that you gave me , still facing the same issue, below is my index.php file

index.php

use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Slim\Exception\NotFoundException;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Selective\BasePath\BasePathDetector;

$container = new Container();
AppFactory::setContainer($container);

$container->set(‘view’, function() {
return Twig::create(’…/tpl’);
});

$app = AppFactory::create();
$app->addRoutingMiddleware();
basePath = (new BasePathDetector(_SERVER))->getBasePath();
$app->setBasePath($basePath);
$app->addErrorMiddleware(true, true, true);
$app->add(TwigMiddleware::createFromContainer($app));

From,
Vino.B

HI Odan,

Was able to resolve this issue , as the folder “static” was under the folder “public”, so moved this folder above the public folder (under the folder “web”) and removed the below lines from index.php, and now I am not getting this error message.

Lines Removed
$app->addRoutingMiddleware();
basePath = (new BasePathDetector(_SERVER))->getBasePath();
$app->setBasePath($basePath);
$app->addErrorMiddleware(true, true, true);

From,
Vino.B

Ok fine :slight_smile:

PS: You could also use the BasePathMiddleware. Example:

use Selective\BasePath\BasePathMiddleware;

// ...
$app = AppFactory::create();
$app->addRoutingMiddleware();
$app->add(new BasePathMiddleware($app));
$app->addErrorMiddleware(true, true, true);
// ...
$app->run();