@@ -369,7 +369,7 @@ public function match($url, $context = array()) {
369
369
unset($ hostOptions ['_port ' ]);
370
370
}
371
371
}
372
-
372
+
373
373
// If no base is set, copy one in.
374
374
if (!isset ($ hostOptions ['_base ' ]) && isset ($ context ['_base ' ])) {
375
375
$ hostOptions ['_base ' ] = $ context ['_base ' ];
@@ -390,17 +390,19 @@ public function match($url, $context = array()) {
390
390
if (array_diff_key ($ defaults , $ url ) !== array ()) {
391
391
return false ;
392
392
}
393
+
394
+ // Defaults with different values are a fail.
395
+ if (array_intersect_key ($ url , $ defaults ) !== $ defaults ) {
396
+ return false ;
397
+ }
398
+
393
399
$ prefixes = Router::prefixes ();
394
400
$ pass = array ();
401
+ $ query = array ();
395
402
396
403
foreach ($ url as $ key => $ value ) {
397
404
// keys that exist in the defaults and have different values is a match failure.
398
405
$ defaultExists = array_key_exists ($ key , $ defaults );
399
- if ($ defaultExists && $ defaults [$ key ] != $ value ) {
400
- return false ;
401
- } elseif ($ defaultExists ) {
402
- continue ;
403
- }
404
406
405
407
// If the key is a routed key, its not different yet.
406
408
if (array_key_exists ($ key , $ keyNames )) {
@@ -418,25 +420,26 @@ public function match($url, $context = array()) {
418
420
}
419
421
420
422
// keys that don't exist are different.
421
- if (!$ defaultExists && !empty ($ value )) {
422
- return false ;
423
+ if (!$ defaultExists && ($ value !== null && $ value !== false && $ value !== '' )) {
424
+ $ query [$ key ] = $ value ;
425
+ unset($ url [$ key ]);
423
426
}
424
427
}
425
428
426
- //if a not a greedy route, no extra params are allowed.
429
+ // if not a greedy route, no extra params are allowed.
427
430
if (!$ this ->_greedy && !empty ($ pass )) {
428
431
return false ;
429
432
}
430
433
431
434
//check patterns for routed params
432
435
if (!empty ($ this ->options )) {
433
436
foreach ($ this ->options as $ key => $ pattern ) {
434
- if (array_key_exists ( $ key , $ url ) && !preg_match ('#^ ' . $ pattern . '$# ' , $ url [$ key ])) {
437
+ if (isset ( $ url[ $ key ] ) && !preg_match ('#^ ' . $ pattern . '$# ' , $ url [$ key ])) {
435
438
return false ;
436
439
}
437
440
}
438
441
}
439
- return $ this ->_writeUrl ($ url , $ pass , $ hostOptions );
442
+ return $ this ->_writeUrl ($ url , $ pass , $ hostOptions, $ query );
440
443
}
441
444
442
445
/**
@@ -447,7 +450,7 @@ public function match($url, $context = array()) {
447
450
* @param array $pass The additional passed arguments.
448
451
* @return string Composed route string.
449
452
*/
450
- protected function _writeUrl ($ params , $ pass = array (), $ hostOptions = array ()) {
453
+ protected function _writeUrl ($ params , $ pass = array (), $ hostOptions = array (), $ query = array () ) {
451
454
if (isset ($ params ['prefix ' ], $ params ['action ' ])) {
452
455
$ params ['action ' ] = str_replace ($ params ['prefix ' ] . '_ ' , '' , $ params ['action ' ]);
453
456
unset($ params ['prefix ' ]);
@@ -495,6 +498,10 @@ protected function _writeUrl($params, $pass = array(), $hostOptions = array()) {
495
498
$ out
496
499
);
497
500
}
501
+ if (!empty ($ query )) {
502
+ $ out = rtrim ($ out , '/ ' );
503
+ $ out .= '? ' . http_build_query ($ query );
504
+ }
498
505
return $ out ;
499
506
}
500
507
0 commit comments