Slim 3 - Always returns Page Not Found - SOLVED

Hi,

I’ve just started to play around with Slim 3 but I’m running into a basic issue. I’m trying to get the code on Slim’s homepage to work as a test but no matter what routes I set up it always returns Page Not Found. I’m using Apache and have this set up in my httpd.conf:

# redirect /api/* to /api/index.php for processing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/api/.* /api/index.php [qsappend,last]

This seems to be working fine as I can use print_r(...) in the /api/index.php and it outputs as expected.

This is the /api/index.php code I’m trying to get to work:

<?php require_once(__DIR__ . '/../../resources/vendor/autoload.php'); ?>
<?php

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

$app = new \Slim\App;
$app->get('/api/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
});
$app->run();

?>

However when ever I try to access http://localhost/api/hello/Richard I just get a “Page Not Found” message. I’m pretty sure the “Page Not Found” page is generated by Slim as it’s not stylized the same way that Apache’s “Not Found” page is. If I do a print_r($_SERVER); in the /api/index.php file, I get these results (I’ve removed some of the un-necessary info):

Array
(
    [SCRIPT_URL] => /api/hello/Richard
    [SCRIPT_URI] => http://localhost/api/hello/Richard
    [HTTP_HOST] => localhost
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_CONNECTION] => keep-alive
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_CACHE_CONTROL] => max-age=0
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache/2.4.25 (Unix) OpenSSL/1.0.2l PHP/7.1.6
    [SERVER_NAME] => localhost
    [REQUEST_SCHEME] => http
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => 
    [REQUEST_URI] => /api/hello/Richard
    [SCRIPT_NAME] => /api/hello/Richard
    [PHP_SELF] => /api/hello/Richard
    [argv] => Array
        (
        )

    [argc] => 0
)

Is there something I’m missing? I’d really like to start playing with the framework but I’m struggling to get even a “Hello World” example running. Any help would be greatly appreciated.

Thank you.

I came across this article where someone was having issues with Slim working in a subdirectory. They were trying to get it to work on an older version of Slim so that issue may not be the same issue that I’m having but it did make me think.

Is there a way to extract the URI that Slim is using to compare against the registered routes?

I’m wondering if there is a discrepancy between what I think Apache is sending to Slim and what Slim is actually using to check against the routes.

This was a nice question for my to puzzle the s*t out of it :slight_smile:
In your index.php 1 line before the run:

$route = $app->getContainer()->get('request')->getUri()->getPath();

Then you have the answer in $route

Hi,

@marcelloh, I didn’t get a chance to use your code as I was able to get my copy finally working but thank you anyway.

It appears as though the issue was with the Apache rewrite rule. I had:

RewriteRule ^/api/.* /api/index.php [qsappend,last]

which did in fact redirect it but it seems Slim couldn’t match my routes to the URI provided. I ultimately changed it to:

RewriteRule ^api/.* /api/index.php [qsappend,last]

and that seemed to get it to work. The only difference was the first / slash needed to be removed. I’m not sure if this was an issue because I was using a subdirectory. Once I figured this out, I was able to actually put a .htaccess file in the /api directory and have everything come together:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [qsappend,last]
</IfModule>

I’m just posting the details in case someone else runs into this and finds this posting useful.

Thanks.