Skip to content

Commit 94ec967

Browse files
authored
Create PostcodeController.php
1 parent 61c9520 commit 94ec967

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Controller/PostcodeController.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/*
3+
* (c) Api Postcode <info@api-postcode.nl>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace ApiPostcode\PostcodeBundle\Controller;
10+
11+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12+
use Symfony\Component\HttpFoundation\JsonResponse;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
/**
17+
* Class PostcodeController
18+
*
19+
* (c) Api Postcode <info@api-postcode.nl>
20+
*/
21+
class PostcodeController extends Controller
22+
{
23+
/**
24+
* @param Request $request
25+
*
26+
* @return JsonResponse
27+
*/
28+
public function fetchAction(Request $request)
29+
{
30+
try {
31+
$postcode = $request->get('zipcode');
32+
$houseNumber = $request->get('number');
33+
34+
$address = $this->get('api.postcode.client')->fetchAddress($postcode, $houseNumber);
35+
36+
return new JsonResponse($address->toArray());
37+
} catch (\Exception $exception) {
38+
return new JsonResponse(['message' => $exception->getMessage()], Response::HTTP_SERVICE_UNAVAILABLE);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)