# Error in hello world

**URL:** <https://discourse.slimframework.com/t/error-in-hello-world/3360>\
**Category:** Questions\
**Created:** [August 16, 2019, 9:07am UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360 "2019-08-16T09:07:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![saronlujan](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@saronlujan](https://discourse.slimframework.com/u/saronlujan)\
**Post date:** [August 16, 2019, 9:07am UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/1 "2019-08-16T09:07:30Z")

</div>

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
<?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();

```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [August 16, 2019, 8:46pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/2 "2019-08-16T20:46:19Z")

</div>

Please try this package to set the Slim basePath:

[Slim 4 BasePath Middleware](https://github.com/selective-php/basepath#installation)

---

<div class="post-metadata">

**Author:** ![EugeneM](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@EugeneM](https://discourse.slimframework.com/u/EugeneM)\
**Post date:** [August 19, 2019, 1:03pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/3 "2019-08-19T13:03:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [August 19, 2019, 5:41pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/4 "2019-08-19T17:41:17Z")

</div>

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

![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/281fd4421849c2e351fe87ff0b8a5b6959e17d04.png)

Result:

![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/0a764588c4ff41aa54c8ce399f8555c2c7a933d0.png)  
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.

---

<div class="post-metadata">

**Author:** ![EugeneM](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@EugeneM](https://discourse.slimframework.com/u/EugeneM)\
**Post date:** [August 19, 2019, 6:52pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/5 "2019-08-19T18:52:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![grayfolk](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/grayfolk/32/932_2.png) [@grayfolk](https://discourse.slimframework.com/u/grayfolk)\
**Post date:** [August 20, 2019, 8:48am UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/6 "2019-08-20T08:48:14Z")

</div>

Dont forget about .htaccess can be disabled: [Apache HTTP Server Tutorial: .htaccess files - Apache HTTP Server Version 2.4](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.

---

<div class="post-metadata">

**Author:** ![saronlujan](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@saronlujan](https://discourse.slimframework.com/u/saronlujan)\
**Post date:** [August 20, 2019, 12:54pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/7 "2019-08-20T12:54:15Z")

</div>

Slim is no longer Slim ☹

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [August 20, 2019, 9:09pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/8 "2019-08-20T21:09:48Z")

</div>

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** :

```php
$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](https://github.com/selective-php/basepath)

---

<div class="post-metadata">

**Author:** ![Manrix](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@Manrix](https://discourse.slimframework.com/u/Manrix)\
**Post date:** [August 24, 2019, 8:25am UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/9 "2019-08-24T08:25:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EugeneM](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@EugeneM](https://discourse.slimframework.com/u/EugeneM)\
**Post date:** [August 24, 2019, 12:10pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/10 "2019-08-24T12:10:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [August 26, 2019, 10:01am UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/11 "2019-08-26T10:01:58Z")

</div>

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](https://discourse.slimframework.com/t/error-in-hello-world/3360/8)

---

<div class="post-metadata">

**Author:** ![EugeneM](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@EugeneM](https://discourse.slimframework.com/u/EugeneM)\
**Post date:** [September 1, 2019, 10:00am UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/12 "2019-09-01T10:00:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tengotutto](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@tengotutto](https://discourse.slimframework.com/u/tengotutto)\
**Post date:** [October 18, 2019, 7:30pm UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/13 "2019-10-18T19:30:04Z")

</div>

> [@EugeneM](#):
>
> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.\*) public/$1 [L]

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

---

<div class="post-metadata">

**Author:** ![EugeneM](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@EugeneM](https://discourse.slimframework.com/u/EugeneM)\
**Post date:** [October 19, 2019, 7:55am UTC](https://discourse.slimframework.com/t/error-in-hello-world/3360/14 "2019-10-19T07:55:18Z")

</div>

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](https://odan.github.io/2019/09/09/slim-4-cheatsheet-and-faq.html)
