10

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.

Prosenjith Roy
  • 179
  • 1
  • 1
  • 9
  • 1
    What url are you trying to visit? What code did you add to the slim app? – MaartenDev Aug 19 '19 at 14:44
  • The information you provided is not enough to identify the problem. Please consider adding more details to your question. Most importantly, the URL you're trying to visit and routes definitions in your code. – Nima Aug 19 '19 at 15:56
  • Please provide the directory that your index.php is in and the URL that you are using that gives the error. – Rob Allen Aug 23 '19 at 07:20

5 Answers5

12

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

Mandien
  • 121
  • 1
  • 6
  • Maybe you should properly tab the rules on your .htaccess code, since the RewriteRule will throw an internal server error ("bad flag delimiters") if it is not on its proper line. –  May 28 '21 at 15:14
  • It's done thx FranP – Mandien Dec 17 '21 at 01:27
  • i've done `$app->addErrorMiddleware(true, true, true); $app->setBasePath('/public');`, my public is in `public/index.php`, but still has same error. – Faraz salehi Jul 31 '23 at 05:33
7

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

catalinp
  • 131
  • 2
  • 5
  • The setBasePath() started to hang my dev machine, which is a pretty well spec-ed one, to a point where a reboot was required. – crafter May 18 '21 at 10:57
  • 1
    This worked for me and is more portable: `app->setBasePath(preg_replace('/(.*)\/.*/', '$1', $_SERVER['SCRIPT_NAME']));` – Chuck Tung Dec 03 '21 at 22:20
0

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.

Mahmood Hussain
  • 423
  • 5
  • 14
0

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

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

coolhack7
  • 1,204
  • 16
  • 35
  • 1
    Clearly not resolving the v4 trouble. See [this answer](https://discourse.slimframework.com/t/slim-4-httpnotfoundexception/3273/20) on the Slim forum – imrok Mar 26 '20 at 14:00