|
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
|
|
|
/** |
|
59
|
|
|
* @Hash("id") |
|
60
|
|
|
* |
|
61
|
|
|
* @param int $id |
|
62
|
|
|
*/ |
|
63
|
|
|
public function encodeLocalized($id): Response |
|
64
|
|
|
{ |
|
65
|
|
|
$url1 = $this->generateUrl('pgs_hash_id_demo_encode_localized', ['id' => $id, '_locale' => 'pl']); |
|
66
|
|
|
$url2 = $this->generateUrl('pgs_hash_id_demo_encode_localized', ['id' => $id]); |
|
67
|
|
|
|
|
68
|
|
|
$response = <<<EOT |
|
69
|
|
|
<html> |
|
70
|
|
|
<body> |
|
71
|
|
|
Provided id: $id<br /> |
|
72
|
|
|
Localized url with encoded parameter and locale provided: <a href="$url1">$url1</a><br /> |
|
73
|
|
|
Localized url with encoded parameter: <a href="$url2">$url2</a><br /> |
|
74
|
|
|
</body> |
|
75
|
|
|
</html> |
|
76
|
|
|
EOT; |
|
77
|
|
|
|
|
78
|
|
|
return new Response($response); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function getDecodeResponse(Request $request, int $id, int $other): string |
|
82
|
|
|
{ |
|
83
|
|
|
$providedId = $this->getRouteParam($request, 'id'); |
|
84
|
|
|
$providedOther = $this->getRouteParam($request, 'other'); |
|
85
|
|
|
|
|
86
|
|
|
$response = <<<EOT |
|
87
|
|
|
<html> |
|
88
|
|
|
<body> |
|
89
|
|
|
Provided id: <b>$providedId</b>, other: <b>$providedOther</b><br /> |
|
90
|
|
|
Decoded id: <b>$id</b>, other: <b>$other</b><br /> |
|
91
|
|
|
</body> |
|
92
|
|
|
</html> |
|
93
|
|
|
EOT; |
|
94
|
|
|
|
|
95
|
|
|
return $response; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getRouteParam(Request $request, $param) |
|
99
|
|
|
{ |
|
100
|
|
|
return $request->attributes->get('_route_params')[$param]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|