@@ -38,28 +38,30 @@ public function load()
3838 $ request ->request ->replace (is_array ($ data ) ? $ data : []);
3939 }
4040
41- $ controllerPath = WORKSPACE . '/controllers ' ;
42-
4341 // #5 - Use the full page path to generate the controller class name
44- $ controllerName = ' Controller ' ;
42+ // #7 - Use a PSR-4 folder structure and build the namespace accordingly
4543 $ currentPagePath = trim (Frontend::instance ()->Page ()->Params ()["current-path " ], '/ ' );
46- foreach (preg_split ("@\/@ " , $ currentPagePath ) as $ part ) {
47- $ controllerName .= ucfirst ($ part );
48- }
44+ $ parts = array_map ("ucfirst " , preg_split ("@\/@ " , $ currentPagePath ));
45+ $ controllerName = "Controller " . array_pop ($ parts );
46+ $ controllerPath = implode ($ parts , "\\" ) . "\\" ;
47+
48+ $ controllerPath = sprintf (
49+ "Symphony\ApiFramework\Controllers \\%s%s " ,
50+ ltrim ($ controllerPath , '\\' ),
51+ $ controllerName
52+ );
4953
5054 // #6 - Check if the controller exists before trying to include it.
5155 // Throw an exception if it cannot be located.
52- $ controllerFullPath = sprintf ("%s/controllers/%s.php " , WORKSPACE , $ controllerName );
53- if (!file_exists ($ controllerFullPath ) || !is_readable ($ controllerFullPath )) {
54- throw new Lib \Exceptions \ControllerNotFoundException ("Controller ' {$ controllerName }' does not exist. " );
56+ if (!class_exists ($ controllerPath )) {
57+ throw new Lib \Exceptions \ControllerNotFoundException ("Controller ' {$ controllerPath }' does not exist. " );
5558 }
5659
57- include_once WORKSPACE . "/controllers/ $ controllerName.php " ;
58- $ controller = new $ controllerName ();
60+ $ controller = new $ controllerPath ();
5961
60- // Make sure the controller extends the AbstractConroller class
62+ // Make sure the controller extends the AbstractController class
6163 if (!($ controller instanceof Lib \AbstractController)) {
62- throw new Lib \Exceptions \ControllerNotValidException ("' {$ controllerName }' is not a valid controller. Check implementation. " );
64+ throw new Lib \Exceptions \ControllerNotValidException ("' {$ controllerPath }' is not a valid controller. Check implementation conforms to Lib\AbstractController . " );
6365 }
6466
6567 $ method = strtolower ($ request ->getMethod ());
0 commit comments