File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 7
7
use Invoker \Invoker ;
8
8
use Invoker \ParameterResolver \AssociativeArrayResolver ;
9
9
use Invoker \ParameterResolver \Container \TypeHintContainerResolver ;
10
+ use Invoker \ParameterResolver \DefaultValueResolver ;
10
11
use Invoker \ParameterResolver \ResolverChain ;
11
12
use Slim \Http \Headers ;
12
13
use Slim \Http \Request ;
62
63
new AssociativeArrayResolver ,
63
64
// Then inject services by type-hints for those that weren't resolved
64
65
new TypeHintContainerResolver ($ c ),
66
+ // Then fall back on parameters default values for optional route parameters
67
+ new DefaultValueResolver (),
65
68
];
66
69
return new Invoker (new ResolverChain ($ resolvers ), $ c );
67
70
},
Original file line number Diff line number Diff line change @@ -42,6 +42,36 @@ public function injects_request_path_parameters()
42
42
$ this ->assertEquals ('Hello matt ' , $ response ->getBody ()->__toString ());
43
43
}
44
44
45
+ /**
46
+ * @test
47
+ */
48
+ public function injects_optional_path_parameter ()
49
+ {
50
+ $ app = new App ;
51
+ $ app ->get ('/[{name}] ' , function ($ response , $ name = null ) {
52
+ $ response ->getBody ()->write ('Hello ' . $ name );
53
+ return $ response ;
54
+ });
55
+
56
+ $ response = $ app ->callMiddlewareStack (RequestFactory::create ('/matt ' ), new Response );
57
+ $ this ->assertEquals ('Hello matt ' , (string ) $ response ->getBody ());
58
+ }
59
+
60
+ /**
61
+ * @test
62
+ */
63
+ public function injects_default_value_in_optional_path_parameter ()
64
+ {
65
+ $ app = new App ;
66
+ $ app ->get ('/[{name}] ' , function ($ response , $ name = 'john doe ' ) {
67
+ $ response ->getBody ()->write ('Hello ' . $ name );
68
+ return $ response ;
69
+ });
70
+
71
+ $ response = $ app ->callMiddlewareStack (RequestFactory::create ('/ ' ), new Response );
72
+ $ this ->assertEquals ('Hello john doe ' , (string ) $ response ->getBody ());
73
+ }
74
+
45
75
/**
46
76
* @test
47
77
*/
You can’t perform that action at this time.
0 commit comments