This repository was archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerService.php
More file actions
59 lines (50 loc) · 1.44 KB
/
ControllerService.php
File metadata and controls
59 lines (50 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace Pianissimo\Component\Framework;
use InvalidArgumentException;
use Pianissimo\Component\Framework\Bridge\Twig\Twig;
use Pianissimo\Component\Framework\Exception\NotFoundHttpException;
use Pianissimo\Component\Routing\Exception\RouteNotFoundException;
use Pianissimo\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ControllerService
{
/**
* @var RouterInterface
*/
private $router;
/**
* @var Twig
*/
private $twig;
public function __construct(
RouterInterface $router,
Twig $twig
) {
$this->router = $router;
$this->twig = $twig;
}
/**
* @throws NotFoundHttpException
*/
public function redirectToRoute(string $routeName): RedirectResponse
{
try {
$route = $this->router->getRoute($routeName);
} catch (RouteNotFoundException $e) {
throw new NotFoundHttpException('404 Not found');
}
return new RedirectResponse($route->getPath());
}
/**
* @throws RouteNotFoundException
* @throws InvalidArgumentException
*/
public function generateUrl(string $routeName, array $parameters): string
{
return $this->router->generateUrl($routeName, $parameters);
}
public function render(string $template, array $data = []): Response
{
return new Response($this->twig->render($template, $data));
}
}