Slim 4 - HttpNotFoundException

Ok, I’m brand new at this framework but have 3+ years of php programming experience, and I struggled for way too long with setting this up. Here’s the TL;DR of this entire conversation (still long but I explain stuff):

At first, aghezzi was trying to add ‘missing files’, but it’s just that the error message looks ugly because no error handling middleware was added to this basic program (it’s never supposed to fail). So you can ignore that part.

Then jDolba correctly identified that it’s an ugly 404 error, which means the server cannot find the page. In this case, there is a problem with our basic program’s path.

Next, several people want you to add to the .htaccess file, but that shouldn’t be necessary for this starter page. We don’t care at this point that we have to type in a long http address. Again, they’re not wrong, but we don’t need to complicate things for our very first slim page. So just ignore the .htaccess stuff for now.

architshin came across one solution: put the whole path in the get() definition:

$app->get('/slimapp/public/index.php/hello/{name}', ...

This works, and is fine. But I preferred Quentin’s solution, personally:

$app = AppFactory::create();

$app->setBasePath("/slimapp/public/index.php");

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

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->run();

I like this because I only have to type the whole path in once for multiple routes. I don’t know if you need to set the base path this way once you start adding to .htaccess, but it works really well for this starter page.

Btw, I’m using XAMPP as my web server, and the default document root is:
C:\xampp\htdocs
so the full path to my starter page is:
C:\xampp\htdocs\slimapp\public\index.php

To test my starter page, the URLs I used were:
localhost/slimapp/public/index.php/ (don’t forget the last ‘/’)
localhost/slimapp/public/index.php/hello/Beautiful