@@ -136,14 +136,18 @@ public function run() {
136136 public function initialize (array $ routes ): self {
137137 foreach ($ routes as $ route ){
138138 $ type = $ route ["type " ] ?? "web " ;
139- $ tmpPrefix = $ route ["prefix " ] ?? "" ;
140- if (!preg_match ("/^\/[a-zA-Z0-9_-]+$/ " , $ tmpPrefix ) && $ tmpPrefix !== "" ){
141- throw new RouterException ("$ tmpPrefix isn't a valid prefix. Prefixes must be like /prefix_name_561 or /prefix-name-120 " , 3004 );
139+ if (!class_exists (\TimePHP \Rest \AbstractRestController::class)) {
140+ throw new RouterException ("You can not use the api type in your routes without installing the following package : timephp/rest " , 3004 );
141+ } else {
142+ $ tmpPrefix = $ route ["prefix " ] ?? "" ;
143+ if (!preg_match ("/^\/[a-zA-Z0-9_-]+$/ " , $ tmpPrefix ) && $ tmpPrefix !== "" ){
144+ throw new RouterException ("$ tmpPrefix isn't a valid prefix. Prefixes must be like /prefix_name_561 or /prefix-name-120 " , 3005 );
145+ }
146+ $ prefix = ($ route ["type " ] === "api " && !array_key_exists ("prefix " , $ route )) ? "/api " : $ tmpPrefix ;
147+ $ method = $ route ["method " ];
148+ $ function = (array_key_exists ("controller " , $ route ) && array_key_exists ("function " , $ route )) ? sprintf ("%s#%s " , $ route ["controller " ], $ route ["function " ]) : $ route ["function " ];
149+ $ this ->$ method ($ prefix .$ route ["url " ], $ function , $ route ["name " ]);
142150 }
143- $ prefix = ($ route ["type " ] === "api " && !array_key_exists ("prefix " , $ route )) ? "/api " : $ tmpPrefix ;
144- $ method = $ route ["method " ];
145- $ function = (array_key_exists ("controller " , $ route ) && array_key_exists ("function " , $ route )) ? sprintf ("%s#%s " , $ route ["controller " ], $ route ["function " ]) : $ route ["function " ];
146- $ this ->$ method ($ prefix .$ route ["url " ], $ function , $ route ["name " ]);
147151 }
148152 return $ this ;
149153 }
0 commit comments