1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pgs\HashIdBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Pgs\HashIdBundle\Annotation\Hash; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @Route('/hash-id/demo') |
13
|
|
|
*/ |
14
|
|
|
class DemoController extends AbstractController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Route("/encode", requirements={"id"="\d+"}) |
18
|
|
|
* |
19
|
|
|
* @param int $id |
20
|
|
|
*/ |
21
|
|
|
public function encode($id): Response |
22
|
|
|
{ |
23
|
|
|
$other = 30; |
24
|
|
|
$url1 = $this->generateUrl('pgs_hash_id_demo_decode', ['id' => $id, 'other' => $other]); |
25
|
|
|
$url2 = $this->generateUrl('pgs_hash_id_demo_decode_more', ['id' => $id, 'other' => $other]); |
26
|
|
|
|
27
|
|
|
$response = <<<EOT |
28
|
|
|
<html> |
29
|
|
|
<body> |
30
|
|
|
Provided id: $id, other: $other <br /> |
31
|
|
|
Url with encoded parameter: <a href="$url1">$url1</a><br /> |
32
|
|
|
Another url with encoded more parameters: <a href="$url2">$url2</a><br /> |
33
|
|
|
</body> |
34
|
|
|
</html> |
35
|
|
|
EOT; |
36
|
|
|
|
37
|
|
|
return new Response($response); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @Route("/decode/{id}/{other}") |
42
|
|
|
* @Hash("id") |
43
|
|
|
*/ |
44
|
|
|
public function decode(Request $request, int $id, int $other): Response |
45
|
|
|
{ |
46
|
|
|
return new Response($this->getDecodeResponse($request, $id, $other)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Route("/decode_more/{id}/{other}") |
51
|
|
|
* @Hash({"id", "other"}) |
52
|
|
|
*/ |
53
|
|
|
public function decodeMore(Request $request, int $id, int $other): Response |
54
|
|
|
{ |
55
|
|
|
return new Response($this->getDecodeResponse($request, $id, $other)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getDecodeResponse(Request $request, int $id, int $other): string |
59
|
|
|
{ |
60
|
|
|
$providedId = $this->getRouteParam($request, 'id'); |
61
|
|
|
$providedOther = $this->getRouteParam($request, 'other'); |
62
|
|
|
|
63
|
|
|
$response = <<<EOT |
64
|
|
|
<html> |
65
|
|
|
<body> |
66
|
|
|
Provided id: <b>$providedId</b>, other: <b>$providedOther</b><br /> |
67
|
|
|
Decoded id: <b>$id</b>, other: <b>$other</b><br /> |
68
|
|
|
</body> |
69
|
|
|
</html> |
70
|
|
|
EOT; |
71
|
|
|
|
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function getRouteParam(Request $request, $param) |
76
|
|
|
{ |
77
|
|
|
return $request->attributes->get('_route_params')[$param]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|