1

I keep getting this error Slim Application Error after installing the new Slim 4 framework.

I tried switching back to old version of slim but I keep getting the same thing.

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

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

/**
 * Instantiate App
 *
 * In order for the factory to work you need to ensure you have installed
 * a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
 * ServerRequest creator (included with Slim PSR-7)
 */
$app = AppFactory::create();

// Add Routing Middleware
$app->addRoutingMiddleware();

/*
 * Add Error Handling Middleware
 *
 * @param bool $displayErrorDetails -> Should be set to false in production
 * @param bool $logErrors -> Parameter is passed to the default ErrorHandler
 * @param bool $logErrorDetails -> Display error details in error log
 * which can be replaced by a callable of your choice.

 * Note: This middleware should be added last. It will not handle any exceptions/errors
 * for middleware added after it.
 */
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

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

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

Output:

Slim Application Error
The application could not run because of the following error:

Details

Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php
Line: 91

Trace:

#0 /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(57): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#2 /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(89): class@anonymous->handle(Object(Slim\Psr7\Request))
#3 /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#4 /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): class@anonymous->handle(Object(Slim\Psr7\Request))

#5 /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/App.php(206): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#6 /opt/lampp/htdocs/MyocappAPI/vendor/slim/slim/Slim/App.php(190): Slim\App->handle(Object(Slim\Psr7\Request))
#7 /opt/lampp/htdocs/MyocappAPI/public/index.php(41): Slim\App->run()
#8 {main}
Artem
  • 3,304
  • 3
  • 18
  • 41
Kyle Mutta
  • 379
  • 1
  • 5
  • 16
  • I don't see any problems in your code. I tested it and it works fine for me. What was the url you were trying to visit? This app only has one route and if you try to visit any url not matching the pattern `/hello/{name}` will result in a `HttpNotFoundException`. – Nima Aug 27 '19 at 10:33
  • We need to know the URL that you are trying to go to. As Nima says: `/hello/Kyle` should work. – Rob Allen Aug 27 '19 at 18:15
  • thanks guys it works fine now – Kyle Mutta Aug 27 '19 at 21:36
  • 1
    Good news is, there is no problem here to be solved. But this also makes your question off-topic. – Nima Aug 28 '19 at 00:54
  • 1
    If you installed Slim 4 in a subdirectory, you may be getting exception as a result of Slim problem with [Base path handling](https://github.com/slimphp/Slim/issues/2512) – piotr_cz Sep 23 '19 at 13:21

2 Answers2

3

I'm glad OP figured it out, but he didn't share his solution so here is what worked for me:

$app = AppFactory::create();

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

where the full path to my starter page is: C:\xampp\htdocs\slimapp\public\index.php

To test my starter page, the URL I used was:

localhost/slimapp/public/index.php/hello/Beautiful

I think in Slim 3 you didn't need to manually set the base path, but you do in Slim 4. I don't know if it's a bug but this worked.

Anazul
  • 231
  • 2
  • 6
0

Slim 4 has some bugs in routing. So,Downgrade the slim version to 3.* Reference: https://discourse.slimframework.com/t/slim-4-httpnotfoundexception/3273/18