Slim 4 Error HttpNotFoundException

Hi All,

Request your help, below is my code, the code is working as expected with any error in the front end, however it throws the below error in the backend log.

Error

[Tue May 19 10:52:48.080893 2020] [proxy_fcgi:error] [pid 16381] [client 16.10.39.72:51740] AH01071: Got error 'PHP message: 404 Not Found\nType: Slim\\Exception\\HttpNotFoundException\nCode: 404\nMessage: Not found.\nFile: /var/project/tech/web/includes/source4/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php\nLine: 93\nTrace: #0 /var/project/tech/web/includes/source4/vendor/slim/slim/Slim/Routing/RouteRunner.php(72): Slim\\Middleware\\RoutingMiddleware->performRouting(Object(Nyholm\\Psr7\\ServerRequest))\n#1 /var/project/tech/web/includes/source4/vendor/slim/twig-view/src/TwigMiddleware.php(125): Slim\\Routing\\RouteRunner->handle(Object(Nyholm\\Psr7\\ServerRequest))\n#2 /var/project/tech/web/includes/source4/vendor/slim/slim/Slim/MiddlewareDispatcher.php(140): Slim\\Views\\TwigMiddleware->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Slim\\Routing\\RouteRunner))\n#3 /var/project/tech/web/includes/source4/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): class@anonymous->handle(Object(Nyholm\\Psr7\\ServerRequest))\n#4 /var/project/saptec...\n', referer: https://devtech.com:8130/

Code

<?php
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Slim\Exception\NotFoundException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Routing\RouteCollectorProxy;

$container = new Container();
AppFactory::setContainer($container);
$container->set('view', function() { return Twig::create('../tpl'); });
$app = AppFactory::create();
$app->add(TwigMiddleware::createFromContainer($app));
$app->addErrorMiddleware(true, true, true);

$app->group('', function(RouteCollectorProxy $group) {
      $group->get('/f101', function($request, $response) {
            $am = new amsrvinfo();
            $SList = $am->getSList();
            return $this->get('view')->render($response, 'page_srv.html', array ("SList" => $SList));
      })->setName('ServerInfo');
	        $group->get('/f102', function($request, $response) {
            $am = new amlocinfo();
            $LList = $am->getSList();
            return $this->get('view')->render($response, 'page_loc.html', array ("LList" => $LList));
      })->setName('ServerInfo');
	        $group->get('/f103', function($request, $response) {
            $am = new amracinfo();
            $RList = $am->getSList();
            return $this->get('view')->render($response, 'page_rac.html', array ("RList" => $RList));
      })->setName('ServerInfo');
});
$app->run();

?>

Hi All,

Tried adding base path and other configuration as stated in the below link , sill no luck
https://github.com/selective-php/basepath

use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Slim\Exception\NotFoundException;
use Slim\Routing\RouteCollectorProxy;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Selective\BasePath\BasePathDetector;
use Selective\BasePath\BasePathMiddleware;
require '../includes/source4/vendor/autoload.php';

$container = new Container();
AppFactory::setContainer($container);
$container->set('view', function() { return Twig::create('../tpl', ['cache' => false]); });
$basePathDetector = new BasePathDetector($_SERVER);
$basePath = $basePathDetector->getBasePath();
$app = AppFactory::create();
$app->add(TwigMiddleware::createFromContainer($app));
$app->addRoutingMiddleware();
$app->add(new BasePathMiddleware($app));
$app->addErrorMiddleware(true, true, true);

Hi All,

Was able to resolve this issue by reordering, I tried to find the base path setting in the Slim4 documentation and was not able to find one, as per this issue it seem that setting basepath is important, so can we add it to the documentation please.

Code

$container = new Container();
AppFactory::setContainer($container);
$container->set('view', function() { return Twig::create('../tpl', ['cache' => false]); });
$app = AppFactory::create();
$app->addRoutingMiddleware();
$basePath = (new BasePathDetector($_SERVER))->getBasePath();
$app->setBasePath($basePath);
$app->add(TwigMiddleware::createFromContainer($app));
$app->addErrorMiddleware(true, true, true);

Hi All,

At last the issue got resolved, the issue was at the apache side.