22namespace ChurchCRM \Slim ;
33
44use ChurchCRM \dto \Photo ;
5+ use ChurchCRM \dto \SystemURLs ;
56use ChurchCRM \Utils \LoggerUtils ;
7+ use Exception ;
8+ use Monolog \Handler \StreamHandler ;
9+ use Monolog \Logger ;
610use Psr \Http \Message \ResponseInterface as Response ;
711use Psr \Http \Message \ServerRequestInterface as Request ;
12+ use Slim \Exception \HttpMethodNotAllowedException ;
813use Slim \Exception \HttpNotFoundException ;
14+ use Slim \HttpCache \CacheProvider ;
915use Slim \Interfaces \RouteInterface ;
16+ use Slim \Psr7 \Response as Psr7Response ;
1017use Slim \Routing \RouteContext ;
11- use Slim \HttpCache \CacheProvider ;
12- use Monolog \Logger ;
13- use Monolog \Handler \StreamHandler ;
1418use Throwable ;
1519
1620
@@ -88,11 +92,33 @@ public static function setupErrorLogger($errorMiddleware)
8892 }
8993
9094 /**
91- * Get Slim base path from environment or default
95+ * Get Slim base path from environment or calculate from SystemURLs root path
96+ * This ensures Slim routes work correctly whether installed at root (/) or in a subdirectory (/churchcrm)
97+ *
98+ * @param string $endpoint The Slim application endpoint (/api or /v2) - REQUIRED
99+ * @return string The complete base path including subdirectory if applicable
92100 */
93- public static function getBasePath ($ default = ' /api ' )
101+ public static function getBasePath (string $ endpoint )
94102 {
95- return getenv ('SLIM_BASE_PATH ' ) ?: $ default ;
103+ // Allow environment override for testing/special deployments
104+ if ($ envPath = getenv ('SLIM_BASE_PATH ' )) {
105+ return $ envPath ;
106+ }
107+
108+ // Get the root path from SystemURLs (configured in Config.php as $sRootPath)
109+ // Examples: '' (root install), '/churchcrm', '/crm', etc.
110+ try {
111+ $ rootPath = SystemURLs::getRootPath ();
112+
113+ // Combine root path with endpoint
114+ // If root is empty string (installed at /), just return endpoint
115+ // If root is /churchcrm, return /churchcrm/api or /churchcrm/v2
116+ return $ rootPath . $ endpoint ;
117+ } catch (Exception $ e ) {
118+ // If SystemURLs not initialized yet, fall back to endpoint only
119+ // This shouldn't happen in normal operation but provides safety
120+ return $ endpoint ;
121+ }
96122 }
97123
98124 /**
@@ -119,13 +145,13 @@ public static function registerDefaultJsonErrorHandler($errorMiddleware)
119145 bool $ logErrorDetails
120146 ) use ($ logger ) {
121147 $ logger ->error ($ exception ->getMessage (), ['exception ' => $ exception ]);
122- $ response = new \ Slim \ Psr7 \ Response ();
148+ $ response = new Psr7Response ();
123149
124150 // Determine appropriate HTTP status code based on exception type
125151 $ statusCode = 500 ;
126- if ($ exception instanceof \ Slim \ Exception \ HttpNotFoundException) {
152+ if ($ exception instanceof HttpNotFoundException) {
127153 $ statusCode = 404 ;
128- } elseif ($ exception instanceof \ Slim \ Exception \ HttpMethodNotAllowedException) {
154+ } elseif ($ exception instanceof HttpMethodNotAllowedException) {
129155 $ statusCode = 405 ;
130156 }
131157
0 commit comments