Slim 4 - HttpNotFoundException

Hey,
I still get Not found error on Slim version 4. Version 3 works perfectly.

My server is running on:
EasyPHP server with PHP version 7.1.3 (https://www.easyphp.org/easyphp-devserver.php)

Full error:
Slim Application Error

The application could not run because of the following error:
Details
Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: D:\personal\slim-learning\test-1\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php
Line: 82
Trace

#0 D:\personal\slim-learning\test-1\vendor\slim\slim\Slim\Routing\RouteRunner.php(52): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 D:\personal\slim-learning\test-1\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(89): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request))
#2 D:\personal\slim-learning\test-1\vendor\slim\slim\Slim\MiddlewareDispatcher.php(123): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#3 D:\personal\slim-learning\test-1\vendor\slim\slim\Slim\MiddlewareDispatcher.php(64): class@anonymous->handle(Object(Slim\Psr7\Request))
#4 D:\personal\slim-learning\test-1\vendor\slim\slim\Slim\App.php(136): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#5 D:\personal\slim-learning\test-1\vendor\slim\slim\Slim\App.php(120): Slim\App->handle(Object(Slim\Psr7\Request))
#6 D:\personal\slim-learning\test-1\index.php(27): Slim\App->run()
#7 {main}

Where can be a problem?

probabilly can’t find
NotFoundException

try to add

use Slim\Exception\NotFoundException;

Thank you for help, but still not working…

it seems you didn’t include autoloader at bootstrap
or files were moved after installation

try to include manually the followin files

web/vendor/slim/slim/Slim/Exception/SlimException.php
/vendor/slim/slim/Slim/Exception/NotFoundException.php

can you share your router configuration + URL you are trying to call?

it is simply Routing Result Not Found

  • means that router din’t found matching route by given URL

Not working, I tried to run php server by console, it worked. Interesting…

It’s this repo: https://packagist.org/packages/adriansuter/slim4-skeleton

Router:
$app->group(’/’, function (RouteCollectorProxy $group) {
$group->get(’’, HomeController::class)->setName(‘home’);
$group->get(‘hello/{name}’, HelloController::class)->setName(‘hello’);
$group->get(‘exception-demo’, ExceptionDemoController::class)->setName(‘exception-demo’);
});

URL:
http://127.0.0.1/edsa-work/personal/slim-learning/test-1-b/public/

As I said upper - I tried to run php server by console, it worked. Interesting…

I am not familiar with EasyPHP server
But in apache you would have to setup Rewrite base for your URL in htaccess

Basically you are setting in your router that URL will be like
‘/hello/Martin’

But in reality you have entire path in URL
‘/edsa-work/personal/slim-learning/test-1-b/public’

Which obviously does not match ‘/hello’

Configure virtual host or use RewriteBase in htaccess

1 Like

It helped, but now i get error - Method not allowed. :smiley:

again, not sure about EasyPHP server

  • doulble check that request sent to server is really GET
var_dump($_SERVER['REQUEST_METHOD']);

Ye, it is GET. It is interesting Slim v.3 is working fine…

I have the same trouble.
I’m using XAMPP 7.3.3 with virtual host for trying Slim4.
My index.php is as follows:

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

require($_SERVER["DOCUMENT_ROOT"]."/firstslim/vendor/autoload.php");
$app = AppFactory::create();

$app->get("/hello", function(ServerRequestInterface $request, ResponseInterface $response, $args) {
	print("Hello world!");
});

$app->run();

.htaccess file:

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

Both files are located at $DOCUMENT_ROOT/firstslim/public/ folder.
I accessed to the following URL.
http://socymslim.localhost/firstslim/public/hello
Then Error has occurred.

**Fatal error** : Uncaught Slim\Exception\HttpNotFoundException: Not found. in /Users/Shinzo/htdocs/socymslim.localhost/firstslim/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php:82 Stack trace:
#0 /Users/Shinzo/htdocs/socymslim.localhost/firstslim/vendor/slim/slim/Slim/Routing/RouteRunner.php(52): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 /Users/Shinzo/htdocs/socymslim.localhost/firstslim/vendor/slim/slim/Slim/MiddlewareDispatcher.php(64): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request))
#2 /Users/Shinzo/htdocs/socymslim.localhost/firstslim/vendor/slim/slim/Slim/App.php(136): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#3 /Users/Shinzo/htdocs/socymslim.localhost/firstslim/vendor/slim/slim/Slim/App.php(120): Slim\App->handle(Object(Slim\Psr7\Request))
#4 /Users/Shinzo/htdocs/socymslim.localhost/firstslim/public/index.php(13): Slim\App->run() 
#5 {main} thrown in  **/Users/Shinzo/htdocs/socymslim.localhost/firstslim/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php**  on line  **82**
1 Like

same issue; read entire thread
you are missing rewrite base in htaccess

Thank you for your reply.
I added RewriteBase to htaccess, yet it doesn’t work.
Finally I figure out the answer.
htaccess file is the same as above. No need to write RewirteBase. Instead, I have to write full path in the route pattern on creating routes as below:

$app->get("/firstslim/public/hello", function(ServerRequestInterface $request, ResponseInterface $response, $args) {
print("Hello world!");
return $response;
});

At first, I wrote
$app->get("/hello", ...
(as I show above) and error has occurred. Strangely, this code works well on Slim3.

it’s working for me as well, but full path in route it’s not good pattern…

Yes. I agree.
In this case, the app is just for testing and learning Slim4. So writing full path for the route pattern is no problem. But for the release apps I must use virtual host and change the document root to public folder.

1 Like

Ok, nice. @architshin, @jDolba & @aghezzi thanks for help!

Hi,
this solution work fine for me without change pattern of routes :

Add $app->setBasePath("/youralias");
After AppFactory::create();

on the .htaccess verifiy this line :
RewriteRule ^ /youralias/index.php [QSA,L]

source: