PHP 瘦身排除目录路径
PHP Slim exclude path to a directory
我使用 PHP Slim 创建了 API。我有一个包含图像的目录,并且...当我想使用(例如)'.../images/index.jpg'时,我看到 404,因为 Slim 正在寻找控制器的路径。你知道如何排除这个目录吗?
编辑
这是我的Routes.php
declare(strict_types=1);
use Slim\App;
return function (App $app) {
//about me
$app->get('/aboutme', [\App\Actions\AboutMeAction::class, "getAll"])->setName('aboutme_get');
$app->patch('/aboutme', [\App\Actions\AboutMeAction::class, "change"])->setName('aboutme_update');
//photos
$app->get('/photos', [\App\Actions\PhotoAction::class, "getAll"])->setName('photo_get_all');
$app->post('/photos', [\App\Actions\PhotoAction::class, "addNew"])->setName('photo_add_new');
$app->delete('/photos/{name}', [\App\Actions\PhotoAction::class, "delete"])->setName('photo_delete');
$app->patch('/photos/{name}', [\App\Actions\PhotoAction::class, "changeFlag"])->setName('photo_change_flag');
$app->get('/photos/download', [\App\Actions\PhotoAction::class, "download"])->setName('photo_download');
//users
$app->post('/login', [\App\Actions\UserAction::class, "signIn"])->setName('login');
//utils
$app->get('/utils', [\App\Actions\UtilsAction::class, "test"])->setName("tests");
};
我的项目结构(仅部分):
├─ config/
│ ├─ bootstrap.php
│ ├─ container.php
│ ├─ middleware.php
│ ├─ routes.php
│ ├─ settings.php
├─ images/
├─ logs/
├─ public/
│ ├─ index.php
├─ src/
│ ├─ Actions/
│ ├─ Domain/
│ │ ├─ Repositories/
│ │ │ ├─ StorageRepository.php
│ │ ├─ Services/
├─ tests/
Sooo...当我想获取图像时,这条路径 example 应该有效...我想知道。
问题出在哪里?
我使用 InfiniteFree
您不希望 Apache 为静态资产调用您的 public/index.php
脚本。 tutorial you followed 似乎涵盖了这一点,所以也许您省略了一些步骤或有复制+粘贴拼写错误:
# Redirect to front controller
RewriteEngine On
# RewriteBase /
# Apply rule only if the request does not match a directory on disk:
RewriteCond %{REQUEST_FILENAME} !-d
# ... and the request does not match a file on disk:
RewriteCond %{REQUEST_FILENAME} !-f
# All good? Then apply the redirection rule:
RewriteRule ^ index.php [QSA,L]
我个人认为 mod_rewrite 非常违反直觉,如果没有必要,我宁愿避免。您可以尝试使用 FallbackResource
directive(恕我直言,出错的可能性较小):
FallbackResource index.php
我使用 PHP Slim 创建了 API。我有一个包含图像的目录,并且...当我想使用(例如)'.../images/index.jpg'时,我看到 404,因为 Slim 正在寻找控制器的路径。你知道如何排除这个目录吗?
编辑
这是我的Routes.php
declare(strict_types=1);
use Slim\App;
return function (App $app) {
//about me
$app->get('/aboutme', [\App\Actions\AboutMeAction::class, "getAll"])->setName('aboutme_get');
$app->patch('/aboutme', [\App\Actions\AboutMeAction::class, "change"])->setName('aboutme_update');
//photos
$app->get('/photos', [\App\Actions\PhotoAction::class, "getAll"])->setName('photo_get_all');
$app->post('/photos', [\App\Actions\PhotoAction::class, "addNew"])->setName('photo_add_new');
$app->delete('/photos/{name}', [\App\Actions\PhotoAction::class, "delete"])->setName('photo_delete');
$app->patch('/photos/{name}', [\App\Actions\PhotoAction::class, "changeFlag"])->setName('photo_change_flag');
$app->get('/photos/download', [\App\Actions\PhotoAction::class, "download"])->setName('photo_download');
//users
$app->post('/login', [\App\Actions\UserAction::class, "signIn"])->setName('login');
//utils
$app->get('/utils', [\App\Actions\UtilsAction::class, "test"])->setName("tests");
};
我的项目结构(仅部分):
├─ config/
│ ├─ bootstrap.php
│ ├─ container.php
│ ├─ middleware.php
│ ├─ routes.php
│ ├─ settings.php
├─ images/
├─ logs/
├─ public/
│ ├─ index.php
├─ src/
│ ├─ Actions/
│ ├─ Domain/
│ │ ├─ Repositories/
│ │ │ ├─ StorageRepository.php
│ │ ├─ Services/
├─ tests/
Sooo...当我想获取图像时,这条路径 example 应该有效...我想知道。 问题出在哪里?
我使用 InfiniteFree
您不希望 Apache 为静态资产调用您的 public/index.php
脚本。 tutorial you followed 似乎涵盖了这一点,所以也许您省略了一些步骤或有复制+粘贴拼写错误:
# Redirect to front controller
RewriteEngine On
# RewriteBase /
# Apply rule only if the request does not match a directory on disk:
RewriteCond %{REQUEST_FILENAME} !-d
# ... and the request does not match a file on disk:
RewriteCond %{REQUEST_FILENAME} !-f
# All good? Then apply the redirection rule:
RewriteRule ^ index.php [QSA,L]
我个人认为 mod_rewrite 非常违反直觉,如果没有必要,我宁愿避免。您可以尝试使用 FallbackResource
directive(恕我直言,出错的可能性较小):
FallbackResource index.php