Skip to content

Commit c9d19c1

Browse files
committed
fix: corrected 404 handling (#3834)
1 parent 4112d66 commit c9d19c1

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

docs/installation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ You can use phpMyFAQ with the following web servers:
3232
- [Apache](http://www.apache.org) 2.4 or later (with mod_rewrite) and mod_ssl (if you wish to run phpMyFAQ under SSL)
3333
- [Nginx](http://www.nginx.org) 1.0 or later (with URL rewriting) and SSL support
3434
- [FrankenPHP](https://frankenphp.dev) 1.0 or later (modern PHP application server built on Caddy)
35+
- [IIS](http://www.iis.net) 7.0 or later (with URL rewriting) and SSL support
3536

3637
#### Apache requirements
3738

phpmyfaq/admin/api/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
$routes = include PMF_SRC_DIR . '/admin-api-routes.php';
3838

3939
$app = new Application($container);
40+
$app->setApiContext(true);
4041
try {
4142
$app->run($routes);
4243
} catch (Exception $exception) {

phpmyfaq/api/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
$routes = include PMF_SRC_DIR . '/api-routes.php';
3737

3838
$app = new Application($container);
39+
$app->setApiContext(true);
3940
try {
4041
$app->run($routes);
4142
} catch (Exception $exception) {

phpmyfaq/src/phpMyFAQ/Application.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Application
4141

4242
private ControllerResolver $controllerResolver;
4343

44+
private bool $isApiContext = false;
45+
4446
public function __construct(
4547
private readonly ?ContainerInterface $container = null,
4648
) {
@@ -70,6 +72,11 @@ public function setControllerResolver(ControllerResolver $controllerResolver): v
7072
$this->controllerResolver = $controllerResolver;
7173
}
7274

75+
public function setApiContext(bool $isApiContext): void
76+
{
77+
$this->isApiContext = $isApiContext;
78+
}
79+
7380
private function setLanguage(): string
7481
{
7582
if (!is_null($this->container)) {
@@ -132,14 +139,37 @@ private function handleRequest(
132139
$response->setStatusCode(Response::HTTP_OK);
133140
$response = call_user_func_array($controller, $arguments);
134141
} catch (ResourceNotFoundException $exception) {
135-
$message = Environment::isDebugMode()
136-
? $this->formatExceptionMessage(
137-
template: 'Not Found: :message at line :line at :file',
138-
exception: $exception,
139-
)
140-
: 'Not Found';
141-
142-
$response = new Response(content: $message, status: Response::HTTP_NOT_FOUND);
142+
// For API requests, return simple text/JSON response
143+
if ($this->isApiContext) {
144+
$message = Environment::isDebugMode()
145+
? $this->formatExceptionMessage(
146+
template: 'Not Found: :message at line :line at :file',
147+
exception: $exception,
148+
)
149+
: 'Not Found';
150+
$response = new Response(content: $message, status: Response::HTTP_NOT_FOUND);
151+
} else {
152+
// For web requests, forward to the PageNotFoundController
153+
try {
154+
$request->attributes->set('_route', 'public.404');
155+
$request->attributes->set(
156+
'_controller',
157+
'phpMyFAQ\Controller\Frontend\PageNotFoundController::index',
158+
);
159+
$controller = $this->controllerResolver->getController($request);
160+
$arguments = $argumentResolver->getArguments($request, $controller);
161+
$response = call_user_func_array($controller, $arguments);
162+
} catch (Throwable $e) {
163+
// Fallback if the controller fails
164+
$message = Environment::isDebugMode()
165+
? $this->formatExceptionMessage(
166+
template: 'Not Found: :message at line :line at :file',
167+
exception: $exception,
168+
)
169+
: 'Not Found';
170+
$response = new Response(content: $message, status: Response::HTTP_NOT_FOUND);
171+
}
172+
}
143173
} catch (UnauthorizedHttpException) {
144174
$response = new RedirectResponse(url: './login');
145175
if (str_contains(haystack: $urlMatcher->getContext()->getBaseUrl(), needle: '/api')) {
@@ -211,7 +241,7 @@ private function handleRequest(
211241
*/
212242
private function formatExceptionMessage(string $template, Throwable $exception): string
213243
{
214-
return strtr(string: $template, from: [
244+
return strtr($template, [
215245
':message' => $exception->getMessage(),
216246
':line' => (string) $exception->getLine(),
217247
':file' => $exception->getFile(),

0 commit comments

Comments
 (0)