Error in hello world :(

I get the error 404:

Slim Application Error

The application could not run because of the following error:

Details

Type: Slim\Exception\HttpNotFoundException

Code: 404

Message: Not found.

File: /Applications/MAMP/htdocs/webspace2/src/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.ph

HTACCESS:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

My Code:

<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

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

// Instantiate App
$app = AppFactory::create();

// Add error middleware
$app->addErrorMiddleware(true, true, true);

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

$app->run();
1 Like

Please try this “workaround” for Apache:

Add this code before $app = AppFactory::create();

// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$basePath = (new BasePathDetector($_SERVER))->getBasePath();
$app->setBasePath($basePath);

Then make sure the content of public/.htaccess looks exactly like this:

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

Also add a second .htaccess file above the public/ directory with this content:

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

Slim 4 Hello World App for Apache

Thank you odan. Your answers are usefull as always.

I tried Slim 4 Hellow world and errors were like the same (Uniform Server Zero 13.4.0).

But why the Slim 4 docs does not have any mention about this?

I tried the Slim 4 Hello World App with Uniform Server Zero 13.4.0. It works.

image

Result:

image
Maybe it’s something related to the local environment. But technically it should work.

But why the Slim 4 docs does not have any mention about this?

That’s a good question. I don’t know.

Yes, odan. Let me clarify little a bit. I mean it works only after your suggestion, but it does not work according the Slim 4 docs. I tested your solution just after your first reply for this topic.
I think your suggestion should be taken into account in the Slim 4 docs. In my opinion, Slim 4, as microframework, should work as expected and noticed in docs, and with minimal configuration.

2 Likes

Dont forget about .htaccess can be disabled: https://httpd.apache.org/docs/current/howto/htaccess.html

The use of .htaccess files can be disabled completely by setting the AllowOverride directive to none:
AllowOverride None

Check this.

Slim is no longer Slim :frowning:

The last days I played with Slim 4 and tried to find a more “elegant” solution for apache users running a Slim 4 app in a documentRoot public/ or a sub-directory of documentRoot.

Then I discovered $app->setBasePath($basePath). You can pass a fix value or let a function (library) detect the real basePath for you.

Example:

$app = AppFactory::create();

// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$basePath = (new BasePathDetector($_SERVER))->getBasePath();
$app->setBasePath($basePath);

Edit: Here you can find the library: selective-php/basepath

1 Like

Same problem here. Just installed slim 4 and i get HttpNotFoundException. Why does it work like this ?

1 Like

It’s done (and works) with little bit work around about Slim 4 docs and with help of @odan suggestions.
Tested on MAMP 5.3 local server.

Project structure

yourproject/
           |_.htaccess
           |_composer.json
           |_composer.lock
           |_public/
           |       |_.htaccess
           |       |_index.php
           |_vendor/

yourproject/.htaccess

// It can be read http://www.slimframework.com/docs/v4/deployment/deployment.html
 <IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^$ public/ [L]
   RewriteRule (^[^/]*$) public/$1 [L]
</IfModule>

// or @odan suggestion
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

yourproject/public/.htaccess

// It can be read http://www.slimframework.com/docs/v4/start/web-servers.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

yourproject/public/index.php

// It can be read http://www.slimframework.com/docs/v4/start/installation.html
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;


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

// start of the @odan suggestion

$path = parse_url($_SERVER['REQUEST_URI'])['path'];
$scriptName = dirname(dirname($_SERVER['SCRIPT_NAME']));
$len = strlen($scriptName);
if ($len > 0 && $scriptName !== '/') {
    $path = substr($path, $len);
}
$_SERVER['REQUEST_URI'] = $path ?: '';

// end of the @odan suggestion

$app = AppFactory::create();

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

$app->run();

Hope. It will help you.

1 Like

There is also a new approach for Slim 4, without modifing $_SERVER['REQUEST_URI'].

Using the .htaccess files in combination with $app->setBasePath($basePath); also works great with urlFor() in Slim 4. Details

Honestly, @odan, only your first suggestion works for me. With modifing $_SERVER[‘REQUEST_URI’]. Maybe it’s only my case?

I copied exactly this code but it didn’t work

Result: Fatal error : Uncaught Slim\Exception\HttpNotFoundException: Not found. i…

In XAMPP 7.3.10

It’s difficult to advise you because of your very short info about your project. The Slim framework is developed dynamically. Now it’s better take a look at https://odan.github.io/2019/09/09/slim-4-cheatsheet-and-faq.html