Slim 4 throws Slim\Exception\HttpNotFoundException

1.8k views Asked by At

I am having this error

error log

I have been the whole day looking for solutions, including trying to set the proper directories as said here, also including the two .htaccess files required, but nothing works

https://github.com/selective-php/basepath#installation

This is my index.php

<?php
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    use Selective\BasePath\BasePathMiddleware;
    use Psr\Http\Message\ResponseInterface;
    use Slim\Exception\HttpNotFoundException;
    use Slim\Factory\AppFactory;
    use Selective\BasePath\BasePathDetector;

    require_once __DIR__ . '/../vendor/autoload.php';

    $app = AppFactory::create();



    // Add Slim routing middleware
    $app->addRoutingMiddleware();

    // Set the base path to run the app in a subdirectory.
    // This path is used in urlFor().

    /* setting this full path was a solution provided by another user here

        https://stackoverflow.com/a/61677171/6791921

    but doesn´t work for me */


    $app->setBasePath("/02.rest-slim-test/public/index.php");
    $app->addErrorMiddleware(true, true, true);

    // Define app routes
    $app->get('/', function (Request $request, Response $response) {
        $response->getBody()->write("Hello, world!");
        return $response;
    });

    // Run app
    $app->run();

?>

Also I will share my project skeleton and the 2 .htaccess anyway, as well as my composer.json

folder structure

.htaccess on the public/ folder:

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

.htaccess on the root folder of my project:

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

composer.json

{
    "require": {
        "slim/slim": "4.*",
        "slim/psr7": "^1.4",
        "selective/basepath": "^2.0"
    }
}

I am using XAMPP with:

Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/8.0.5

loaded modules as phpinfo() says

mods enabled apache

mod_rewrite module is enabled as phpinfo() says

Also I have tried to change this apache config httpd.conf directives but nothing works, since I have seen somewhere, but doesn´t work.

Now it is so. With the rest of projects I have no problem.

<Directory />
    AllowOverride none
    Require all denied
</Directory>

I don´t know which more relevant info could I say.

Other resources I looked at:

PHP Slim4 on Apache2 gives HttpNotFoundException

Slim\Exception\HttpNotFoundException

https://odan.github.io/2019/11/05/slim4-tutorial.html

1

There are 1 answers

1
AudioBubble On BEST ANSWER

OK, I managed to solve it, this is the code which worked. The main change is on the path parameter on SetBasePath()- Despite I tried the same before, now it works. Just in case it is useful for someone.

<?php
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    use Selective\BasePath\BasePathMiddleware;
    use Psr\Http\Message\ResponseInterface;
    use Slim\Exception\HttpNotFoundException;
    use Slim\Factory\AppFactory;
    use Selective\BasePath\BasePathDetector;

    require_once __DIR__ . '/../vendor/autoload.php';



    $app = AppFactory::create();



    // Add Slim routing middleware
    $app->addRoutingMiddleware();


    $app->setBasePath("/02.rest-slim-test/public");

    $app->addErrorMiddleware(true, true, true);

    // Define app routes
    $app->get('/', function (Request $request, Response $response) {
        $response->getBody()->write("Hello, world!");
        return $response;
    });

     // Run app
     $app->run();

?>