Apache2上的PHP Slim4提供HttpNotFoundException

2022-06-13 00:00:00 php slim .htaccess apache

我看到的链接包括:

https://akrabat.com/running-slim-4-in-a-subdirectory/

https://discourse.slimframework.com/t/slim-4-httpnotfoundexception/3273/18

和其他一些

我正在尝试在apache2上设置Slim4项目。但尝试访问它时出现错误

PHP致命错误:未捕获SlimExceptionHttpNotFoundException:未找到...

我的索引文件位于/var/www/html/_projects/work/calc1/src

以下是我在/etc/apache2/sites-available中的calc.conf:

<VirtualHost *:80>
    ServerName calc.mrid.local
    ServerAlias calc.mrid.local

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/_projects/work/calc1/src

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

这是我的.htaccess,位于src目录

RewriteEngine On
RewriteBase /_projects/work/calc1/src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule ^(.*).html$ /$1 [L,R=301]

我的index.php如下所示:

// use and require statements...

$container = new Container();
AppFactory::setContainer($container);

$container->set('view', function() {
    return SlimViewsTwig::create(__DIR__ . '/public/templates');
});

$app = AppFactory::create();

// even without this line it doesn't work
$app->setBasePath("/_projects/work/calc1/src"); 

$app->add(TwigMiddleware::createFromContainer($app));

// my routes....

$app->run();

有人在Apache2上使用过Slim4吗?如果是,你能给我指路吗?

php

src/目录实际上只针对推荐答案类,不能通过Web访问。ApacheDocumentRoot应该指向包含您的前端控制器public/index.phppublic/目录。请看Standard PHP package skeleton。有关斯利姆和阿帕奇的更多详细信息,请参阅Slim 4 Tutorial。

相关文章