I have a problem with the operation of the website.
I installed slim framework v.4 I have php v.8.1 and the server IIS Slim 4 Documentation - Slim Framework I used this code to try if the project would start and unfortunately after many different attempts I was unable to start the website correctly.
I searched the forums but it still does not work. Has anyone encountered this problem before on the IIS server?
I believe that the cause may be this server and it will have to be configured accordingly, please advise.
I used url: http://localhost/TestSlim/index.php/. I am trying different paths without index.php : http://localhost/TestSlim/ or http://localhost/TestSlim/index.php/hello few times I reinstaled slim but without success. I am not sure how it works with IIS.
I used different methods I saw that problem isn’t exist for php 7.3.3 but higher like 8.1 is a problem with that
This is my index.php:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/vendor/autoload.php';
//echo "test";
$app = AppFactory::create();
$app->addRoutingMiddleware();
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$app->setBasePath('localhost/TestSlim');
echo $app->getBasePath();
$app->get('/', 'hello');
// Run app
$app->run();
?>
The basePath, if any, should start with a “/”. I would try to commend out this line or use the basePath from your URI path. Otherwise try to install the BasePath middleware for auto-detection.
I want to continue this issue because on the same configuration I was trying to use method DELETE and PUT. Not works form me only POST and GET.
$app->delete('api/hello/remove/{id}', function ($request, $response, $args) {
$id = $args['id'];
$response->getBody()->write("Item with ID $id has been deleted.");
return $response->withStatus(200);
});
$app->put('api/hello/put/{id}', function ($request, $response, $args) {
$id = $args['id'];
$response->getBody()->write("Item with ID $id has been deleted.");
return $response->withStatus(200);
});
I was trying to add some allows for DELETE and PUT in IIS configurations but nothing happened still the same issue. Full code below:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Middleware\MethodOverrideMiddleware;
require __DIR__ . '/vendor/autoload.php';
//echo "test";
$app = AppFactory::create();
$app->setBasePath('/TestSlim');
$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
$methodOverrideMiddleware = new MethodOverrideMiddleware();
$app->add($methodOverrideMiddleware);
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
//$app->setBasePath('localhost/TestSlim');
//echo $app->getBasePath();
$app->get('/api/', function (Request $request, Response $response) {
$response->getBody()->write("Welcome to Slim 4 on IIS!");
return $response;
});
// Hello Route
$app->get('/api/hello', function (Request $request, Response $response) {
$response->getBody()->write("Hello, Slim!");
return $response;
});
$app->post('/api/hello', function (Request $request, Response $response) {
$response->getBody()->write("Hello, Slim!");
return $response;
});
$app->add(function ($request, $handler) {
return $handler->handle($request);
});
$app->delete('api/hello/remove/{id}', function ($request, $response, $args) {
$id = $args['id'];
$response->getBody()->write("Item with ID $id has been deleted.");
return $response->withStatus(200);
});
$app->put('api/hello/put/{id}', function ($request, $response, $args) {
$id = $args['id'];
$response->getBody()->write("Item with ID $id has been deleted.");
return $response->withStatus(200);
});
// Run app
$app->run();
?>