@@ -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