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.

1 Like

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:

1 Like

Thanks, this is helpful it worked for me after hours of troubleshooting

1 Like

Ok, I’m brand new at this framework but have 3+ years of php programming experience, and I struggled for way too long with setting this up. Here’s the TL;DR of this entire conversation (still long but I explain stuff):

At first, aghezzi was trying to add ‘missing files’, but it’s just that the error message looks ugly because no error handling middleware was added to this basic program (it’s never supposed to fail). So you can ignore that part.

Then jDolba correctly identified that it’s an ugly 404 error, which means the server cannot find the page. In this case, there is a problem with our basic program’s path.

Next, several people want you to add to the .htaccess file, but that shouldn’t be necessary for this starter page. We don’t care at this point that we have to type in a long http address. Again, they’re not wrong, but we don’t need to complicate things for our very first slim page. So just ignore the .htaccess stuff for now.

architshin came across one solution: put the whole path in the get() definition:

$app->get('/slimapp/public/index.php/hello/{name}', ...

This works, and is fine. But I preferred Quentin’s solution, personally:

$app = AppFactory::create();

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

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

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

$app->run();

I like this because I only have to type the whole path in once for multiple routes. I don’t know if you need to set the base path this way once you start adding to .htaccess, but it works really well for this starter page.

Btw, I’m using XAMPP as my web server, and the default document root is:
C:\xampp\htdocs
so the full path to my starter page is:
C:\xampp\htdocs\slimapp\public\index.php

To test my starter page, the URLs I used were:
localhost/slimapp/public/index.php/ (don’t forget the last ‘/’)
localhost/slimapp/public/index.php/hello/Beautiful