@@ -68,6 +68,52 @@ public function register_route_callback($route_string, $method, $defaults = arra
68
68
$ route ->callback = $ method ;
69
69
self ::$ routes [] = $ route ;
70
70
}
71
+
72
+ public function register_split_route ($ params ) {
73
+
74
+ $ component = isset ($ params ["component " ]) ? $ params ["component " ] : "" ;
75
+ $ controllers = isset ($ params ["controllers " ]) ? $ params ["controllers " ] : array ($ component );
76
+ $ action = isset ($ params ["action " ]) ? $ params ["action " ] : "index " ;
77
+ $ extra = isset ($ params ["extra " ]) ? $ params ["extra " ] : array ();
78
+
79
+ if (!$ component || count ($ controllers ) == 0 ) { return ; }
80
+ // untested
81
+ // foreach($extra as $key => $value) {
82
+ // $route = "/$component/:controller/:action";
83
+ // SilkRoute::register_route($route, $defaults);
84
+ // }
85
+
86
+ if ( count ($ controllers ) > 1 ) {
87
+
88
+ $ defaults = array ( "component " => $ component , "action " => $ action );
89
+
90
+ foreach ($ controllers as $ one_controller ) {
91
+ $ defaults ["controller " ] = $ one_controller ;
92
+
93
+ $ route = "/ $ component/ $ one_controller/:action " ;
94
+ SilkRoute::register_route ($ route , $ defaults );
95
+
96
+ $ route = "/ $ component/ $ one_controller " ;
97
+ SilkRoute::register_route ($ route , $ defaults );
98
+
99
+ if ( $ component != $ one_controller ) {
100
+ $ route = "/ $ one_controller/:action " ;
101
+ SilkRoute::register_route ($ route , $ defaults );
102
+
103
+ $ route = "/ $ one_controller " ;
104
+ SilkRoute::register_route ($ route , $ defaults );
105
+ }
106
+ }
107
+ } else {
108
+ $ defaults = array ( "component " => $ component , "controller " => $ controllers [0 ], "action " => $ action );
109
+
110
+ $ route = "/ $ controllers [0 ]/:action " ;
111
+ SilkRoute::register_route ($ route , $ defaults );
112
+
113
+ $ route = "/ $ controllers [0 ]" ;
114
+ SilkRoute::register_route ($ route , $ defaults );
115
+ }
116
+ }
71
117
72
118
public static function match_route ($ uri , $ route_shortening = true )
73
119
{
@@ -82,11 +128,7 @@ public static function match_route($uri, $route_shortening = true)
82
128
83
129
foreach (self ::$ routes as $ one_route )
84
130
{
85
- $ route_string = $ one_route ->route_string ;
86
- if ($ route_shortening === true )
87
- $ route_string = self ::rebuild_route ($ route_string , $ uri );
88
-
89
- $ regex = self ::create_regex_from_route ($ route_string );
131
+ $ regex = self ::create_regex_from_route ($ one_route ->route_string );
90
132
if (preg_match ($ regex , $ uri , $ matches ))
91
133
{
92
134
$ defaults = $ one_route ->defaults ;
@@ -169,47 +211,19 @@ public static function build_default_component_routes()
169
211
170
212
foreach ($ components as $ component =>$ controllers )
171
213
{
172
- $ count = 0 ;
173
- foreach ($ controllers as $ one_controller )
174
- {
175
- $ count ++;
176
- $ class_name = str_replace ("class. " , "" , str_replace (".php " , "" , str_replace ("_controller " , "" , $ one_controller )));
177
-
178
- if ( count ( $ controllers ) > 1 ) {
179
- //create component route first so /component matches properly
180
- $ key = "/ $ component/ $ component/:action/:id " ;
181
- if ( !array_key_exists ($ key , $ route )) {
182
- $ route [$ key ] = array (
183
- "component " => $ component ,
184
- "controller " => $ component ,
185
- "action " => "index " ,
186
- "id " => ""
187
- );
188
- }
189
-
190
- $ key = "/ $ component/ $ class_name/:action/:id " ;
191
- if ( !array_key_exists ($ key , $ route )) {
192
- $ route [$ key ] = array (
193
- "component " => $ component ,
194
- "controller " => $ class_name ,
195
- "action " => "index " ,
196
- "id " => ""
197
- );
198
- }
199
-
200
- $ key = "/ $ class_name/:action/:id " ;
201
- if ( !array_key_exists ($ key , $ route )) {
202
- $ route [$ key ] = array (
203
- "component " => $ component ,
204
- "controller " => $ class_name ,
205
- "action " => "index " ,
206
- "id " => ""
207
- );
208
- }
209
- }
214
+ if ( file_exists ( join_path ( ROOT_DIR , "components " , $ component , "routes.php " ) ) ) {
215
+ include_once ( join_path ( ROOT_DIR , "components " , $ component , "routes.php " ) );
216
+ }
217
+ $ class_names = array ();
218
+ foreach ( $ controllers as $ one_controller ) {
219
+ $ class_names [] = str_replace ("class. " ,"" , str_replace ("_controller.php " , "" , $ one_controller ));
210
220
}
221
+ self ::register_split_route (array ( "component " => $ component , "controllers " => $ class_names ));
222
+
211
223
}
212
- $ route ["/:controller/:action/:id " ] = array ("component " => $ component , "action " => "index " );
224
+ $ route ["/:controller/:action/:id " ] = array ("action " => "index " );
225
+ $ route ["/:controller/:action " ] = array ("action " => "index " );
226
+ $ route ["/:controller " ] = array ("action " => "index " );
213
227
foreach ( $ route as $ route_string => $ params ) {
214
228
SilkRoute::register_route ($ route_string , $ params );
215
229
}
@@ -221,4 +235,4 @@ class SilkRouteNotMatchedException extends Exception
221
235
}
222
236
223
237
# vim:ts=4 sw=4 noet
224
- ?>
238
+ ?>
0 commit comments