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.