Van4kk
Van4kk

Reputation: 59

PHP Slim\Exception\HttpNotFoundException 404 Not Found and nothing is helping

I'm creating an web app, and I can't move because of Slim 4. It's showing up this error:

Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:91 Stack trace: #0 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\Routing\RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #2 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #4 C:\xampp\htdocs\projectfolder\app\index.php(16): Slim\App->run() #5 {main} thrown in C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 91

I'm using xampp, there is apache server and I thought the problem was there, in .htacces file, but hell no..

here is how it looks

error

I've been trying to fix this for more than 4-5 hours, I tried everything I found on google, stackoverflow, Slim's Github, YouTube.. nothing is working.

.htacces

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

My directory

folders

composer.json

json

index.php below:

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

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

$app->run();

I do not know what to do anymore, maybe someone can help me and other thousands of people who can't find the answer, or it will be better idea to move and trying another framework..

Upvotes: 2

Views: 8475

Answers (2)

D Luwaga
D Luwaga

Reputation: 56

Set A URL base path detector for Slim 4. Follow steps here https://github.com/selective-php/basepath

<?php

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

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

$app = AppFactory::create();

// Add Slim routing middleware
$app->addRoutingMiddleware();

// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$app->add(new BasePathMiddleware($app));

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

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

// Run app
$app->run();
?> 

That should work properly.

Upvotes: 4

odan
odan

Reputation: 4952

I see some possible issues that cause the problem.

  • The vendor/ directory belongs into the project-root directory
  • There is a typo in scr/, it should be src/
  • The index.php (front controller) should be placed in a separate directory, e.g. public/
  • You are running your app in sub-directory of the webservers documentRoot. So you need a second .htaccess file in the project root directory.
  • Then you need to configure the Slim basePath to 'projectFolder/' or you use the BasePathMiddleware for this purpose.

In my blog post Slim Framework Tutorial I explain this all in detail.

Upvotes: 0

Related Questions