Skip to content

Commit 519bb4b

Browse files
committed
Merge pull request dingo#622 from pulkitjalan/patch-1
Added `any` and `match` router functions
2 parents 5330703 + 0e70fc3 commit 519bb4b

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/Routing/Router.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,35 @@ public function options($uri, $action)
253253
return $this->addRoute('OPTIONS', $uri, $action);
254254
}
255255

256+
/**
257+
* Create a new route that responding to all verbs.
258+
*
259+
* @param string $uri
260+
* @param array|string|callable $action
261+
*
262+
* @return mixed
263+
*/
264+
public function any($uri, $action)
265+
{
266+
$verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'];
267+
268+
return $this->addRoute($verbs, $uri, $action);
269+
}
270+
271+
/**
272+
* Create a new route with the given verbs.
273+
*
274+
* @param array|string $methods
275+
* @param string $uri
276+
* @param array|string|callable $action
277+
*
278+
* @return mixed
279+
*/
280+
public function match($methods, $uri, $action)
281+
{
282+
return $this->addRoute(array_map('strtoupper', (array) $methods), $uri, $action);
283+
}
284+
256285
/**
257286
* Register an array of resources.
258287
*

tests/Routing/RouterTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,70 @@ public function testNoGroupVersionThrowsException()
8282
});
8383
}
8484

85+
public function testMatchRoutes()
86+
{
87+
$this->router->version('v1', function ($api) {
88+
$api->match(['get', 'post'], 'foo', function () {
89+
return 'bar';
90+
});
91+
});
92+
93+
$this->router->setConditionalRequest(false);
94+
95+
$response = $this->router->dispatch(
96+
$request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json'])
97+
);
98+
99+
$this->assertEquals(200, $response->getStatusCode());
100+
$this->assertEquals('bar', $response->getContent());
101+
102+
$response = $this->router->dispatch(
103+
$request = $this->createRequest('foo', 'POST', ['accept' => 'application/vnd.api.v1+json'])
104+
);
105+
106+
$this->assertEquals(200, $response->getStatusCode());
107+
$this->assertEquals('bar', $response->getContent());
108+
}
109+
110+
public function testAnyRoutes()
111+
{
112+
$this->router->version('v1', function ($api) {
113+
$api->any('foo', function () {
114+
return 'bar';
115+
});
116+
});
117+
118+
$this->router->setConditionalRequest(false);
119+
120+
$response = $this->router->dispatch(
121+
$request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json'])
122+
);
123+
124+
$this->assertEquals(200, $response->getStatusCode());
125+
$this->assertEquals('bar', $response->getContent());
126+
127+
$response = $this->router->dispatch(
128+
$request = $this->createRequest('foo', 'POST', ['accept' => 'application/vnd.api.v1+json'])
129+
);
130+
131+
$this->assertEquals(200, $response->getStatusCode());
132+
$this->assertEquals('bar', $response->getContent());
133+
134+
$response = $this->router->dispatch(
135+
$request = $this->createRequest('foo', 'PATCH', ['accept' => 'application/vnd.api.v1+json'])
136+
);
137+
138+
$this->assertEquals(200, $response->getStatusCode());
139+
$this->assertEquals('bar', $response->getContent());
140+
141+
$response = $this->router->dispatch(
142+
$request = $this->createRequest('foo', 'DELETE', ['accept' => 'application/vnd.api.v1+json'])
143+
);
144+
145+
$this->assertEquals(200, $response->getStatusCode());
146+
$this->assertEquals('bar', $response->getContent());
147+
}
148+
85149
public function testRouterPreparesNotModifiedResponse()
86150
{
87151
$this->router->version('v1', function () {

0 commit comments

Comments
 (0)