'Slim\Exception\HttpNotFoundException

I'm creating a new Slim project and getting the following error: Slim Application Error: The application could not run because of the following error:

Error Details

Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php
Line: 91

Trace

#0 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(57): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(124): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#2 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(89): class@anonymous->handle(Object(Slim\Psr7\Request))
#3 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(124): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#4 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): class@anonymous->handle(Object(Slim\Psr7\Request))
#5 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\App.php(174): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#6 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\App.php(158): Slim\App->handle(Object(Slim\Psr7\Request))
#7 C:\xampp\htdocs\MyApi\public\index.php(18): Slim\App->run()
#8 {main}

Here is my index.php

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

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

$app = AppFactory::create();
$app->addRoutingMiddleware();
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

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

$app->run();

This might be a basic problem.But I am new.Need help please.



Solution 1:[1]

To solve the problem with Slim 4 here is what I did :

1/ Just after

$app = AppFactory::create();

I added :

$app->setBasePath("/myapp/api"); // /myapp/api is the api folder (http://domain/myapp/api)

2/ In my .htaccess :

RewriteEngine on  
RewriteCond %{ENV:REDIRECT_STATUS} !200 
RewriteRule ^api/(.*)$ api/index.php/$1

3/ Finaly to handle the 'HttpNotFoundException' Slim Exception I added a try/catch (I don't know why Slim doesn't handle it internaly => maybe to give us more possibility?)

try {
    $app->run();     
} catch (Exception $e) {    
  // We display a error message
  die( json_encode(array("status" => "failed", "message" => "This action is not allowed"))); 
}

Hope it can help

** Edited => To force Slim to handle Exceptions, after : **

$app = AppFactory::create();

add :

$app->addErrorMiddleware(true, true, true);

Ref : http://www.slimframework.com/docs/v4/middleware/body-parsing.html

Solution 2:[2]

This worked for me (based on Mandien's answer).

I've just added index.php in setBasePath(...), no .htaccess needed.

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

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

http://localhost/myapp/public/index.php/hello/world

shows Hello, world

Linux, Apache, Slim 4

Solution 3:[3]

I was getting the same error, let me describe what I was exactly doing and then how did it work!
I was running local server using the command on terminal php -S localhost:8002
The command returned Document root is /Users/Codeus/Desktop/ and error occured when I navigated to localhost:8002

Then again I ran localhost from the directory where index.php or whatever your is that you wanna run and it worked fine for me. For example, for me, it was two levels up then command php -S localhost:8002 returned Document root is /Users/Codeus/Desktop/myslim/src/public and this is exactly where my index.php file was. Hope this works for you as well.

Solution 4:[4]

If you are using slim and a newbie then try this code and it will work

Simply change your code a little. Right now, you all have this code:

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) { ... }

Change it to full naming convention:

$app->get('/[my-appname]/public/hello/{name}', function (Request $request, Response $response, array $args) { .. }

Full 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';


 $app = AppFactory::create();

//here i used the url change to your project name on [my-appname]
 $app->get('/[my-appname]/public/hello/{name}', function (Request $request, 
 Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
 });

$app->run();

Solution 5:[5]

Open CMD in your Project Root - Type

composer require slim/slim:3.*

Then It Will Update then refresh the browser that's it.

Solution 6:[6]

After nearly 3 hours of intense browsing, I have come up with the solution which would be to downgrade the version of Slim framework from the project's root directory. I was using v4.3.0 which due to some strange reasons didn't want to behave the way it is meant to and the resolution for which was beyond my understanding. The steps would be as follows:

  • Go to the project root directory and type cmd in the file path for the command prompt window.
  • Type in composer require slim/slim:3.*

  • Once done, you would then need to replace the pre-existing code on
    public/index.php from your code editor with the below:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
//use Slim\Factory\AppFactory;

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

$app = new \Slim\App;

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

$app->run();

The above snippet is basically the intro page code but from Slim v3.12.3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Mahmood Hussain
Solution 4 Muhammad Shamshad
Solution 5 Tasnuva Tavasum oshin
Solution 6