Slim 4 - HttpNotFoundException

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

following this and i still get this error:

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

Here is how I made it sing:

  • root folder in apache24/htdocs/ accessible as localhost/

  • slim in localhost/vendor

  • project in localhost/

  • .htaccess in root folder (localhost/)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
  • index.php in root folder
<?php
use Slim\Factory\AppFactory;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

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

$app = AppFactory::create();

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

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

$app->run();

If I move .htaccess and index.php to a public folder located in my web root, then the index.php should be like this:

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

require __DIR__ . '/../vendor/autoload.php';         <====== CHANGE HERE 

$app = AppFactory::create();
$app->setBasePath("/public");                        <====== CHANGE HERE 

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

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

$app->run();

I had similar errors to those mentioned by others and the problem was that although the mod_rewrite module of Apache was enabled, it was not working because the AllowOverride setting in httpd.conf of Apache is set to None by default. You should leave this to None for the global setting (for security - as suggested), but you should change it to “AllowOverride All” for the virtual host you are working with (that is: <Directory “${SRVROOT}/htdocs”> for the default localhost).

This is stated in the documentation here:


but people just follow the basic installation guidelines of SLIM that do not mention this. It took a whole day to figure it out! Moreover, most examples use the local PHP server starting it from inside the public folder. It is different if you use Apache serving from the localhost.

The above should work with any virtual host defined in Apache.

Thanks for this. :+1:t2:

1 Like

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

how to resolve this issue?. .

this is helpful it worked for me after hours of troubleshooting. .