Slim Framework 4 with php 8.1 and IIS Server comunication - Stack Overflow

I have a problem with the operation of the website.I installed slim framework v.4 I have php v.8.1 and

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 / 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();
?>

And I see the following error:

404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: C:\TestSlimFramework\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php Line: 91 Trace: #0 C:\TestSlimFramework\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 C:\TestSlimFramework\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner)) #2 C:\TestSlimFramework\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 C:\TestSlimFramework\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 C:\TestSlimFramework\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 C:\TestSlimFramework\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #6 C:\TestSlimFramework\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #7 C:\TestSlimFramework\index.php(23): Slim\App->run() #8 {main} 

I already deployed on IIS using Server->Sites->Default Web Site->Add Virtual Directory:

enter image description here
and added simple structure from directory

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 https://www.slimframework/docs/v4/ 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();
?>

And I see the following error:

404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: C:\TestSlimFramework\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php Line: 91 Trace: #0 C:\TestSlimFramework\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 C:\TestSlimFramework\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner)) #2 C:\TestSlimFramework\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 C:\TestSlimFramework\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 C:\TestSlimFramework\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 C:\TestSlimFramework\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #6 C:\TestSlimFramework\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #7 C:\TestSlimFramework\index.php(23): Slim\App->run() #8 {main} 

I already deployed on IIS using Server->Sites->Default Web Site->Add Virtual Directory:

enter image description here
and added simple structure from directory

Share Improve this question asked Mar 3 at 11:19 Piotr WPiotr W 133 bronze badges 8
  • Slim has its own support channel, discourse.slimframework. – Lex Li Commented Mar 4 at 3:34
  • have you tried to add the Slim site as a new site in iis? and while adding the folder path use the public folder which you can find under the project root folder e.g. TestSlim\public – Jalpa Panchal Commented Mar 5 at 8:06
  • It is doesn't work for me – Piotr W Commented Mar 5 at 9:49
  • Have you tried the set it up as shown in the Slim docs? slimframework/docs/v4/start/web-servers.html#iis – odan Commented Mar 5 at 14:09
  • Yes I already did that and the same issue – Piotr W Commented Mar 7 at 9:50
 |  Show 3 more comments

1 Answer 1

Reset to default 0

I have created this below sample project to implement your requitement:

I have installed the below component:

composer require slim/slim:"4.*"

composer require slim/psr7

composer require nyholm/psr7 nyholm/psr7-server

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';
$app = AppFactory::create();
// Set Base Path for Virtual Directory
//$app->setBasePath('/slimtest'); 
// Middleware
require __DIR__ . '/src/Middleware.php';
// Include Routes
require __DIR__ . '/src/Routes.php';
// Run App
$app->run();

src\Routes.php:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
global $app;
// Home Route
$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write("Welcome to Slim 4 on IIS!");
    return $response;
});
// Hello Route
$app->get('/hello', function (Request $request, Response $response) {
    $response->getBody()->write("Hello, Slim!");
    return $response;
});
// JSON API Example
$app->get('/api/data', function (Request $request, Response $response) {
    $data = ['message' => 'Hello from Slim 4 API'];
    $response->getBody()->write(json_encode($data));
    return $response->withHeader('Content-Type', 'application/json');
});

src\Middleware.php:

<?php
global $app;
$app->addRoutingMiddleware();
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="slimrule" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Set base path only when you use virtual directory or subapplication in iis: $app->setBasePath('/slimtest');

Result:

As website:

As a virtual directory :

I used the root folder of the application as iis path.

Make sure the web.config is placed in the project root folder.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745097163a4611049.html

相关推荐

  • Slim Framework 4 with php 8.1 and IIS Server comunication - Stack Overflow

    I have a problem with the operation of the website.I installed slim framework v.4 I have php v.8.1 and

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信