1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tadcka package. |
5
|
|
|
* |
6
|
|
|
* (c) Tadas Gliaubicas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tadcka\Bundle\RoutingBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
use Tadcka\Component\Routing\Model\Manager\RedirectRouteManagerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Tadas Gliaubicas <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @since 8/28/14 12:23 PM |
23
|
|
|
*/ |
24
|
|
|
class RedirectController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var RedirectRouteManagerInterface |
28
|
|
|
*/ |
29
|
|
|
private $redirectRouteManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RouterInterface |
33
|
|
|
*/ |
34
|
|
|
private $router; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param RedirectRouteManagerInterface $redirectRouteManager |
40
|
|
|
* @param RouterInterface $router |
41
|
|
|
*/ |
42
|
|
|
public function __construct(RedirectRouteManagerInterface $redirectRouteManager, RouterInterface $router) |
43
|
|
|
{ |
44
|
|
|
$this->router = $router; |
45
|
|
|
$this->redirectRouteManager = $redirectRouteManager; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Action to redirect based on a RedirectRouteInterface route. |
50
|
|
|
* |
51
|
|
|
* @param string $redirectRouteName |
52
|
|
|
* |
53
|
|
|
* @return RedirectResponse |
54
|
|
|
* |
55
|
|
|
* @throws NotFoundHttpException |
56
|
|
|
*/ |
57
|
|
|
public function redirectAction($redirectRouteName) |
58
|
|
|
{ |
59
|
|
|
$redirectRoute = $this->redirectRouteManager->findByName($redirectRouteName); |
60
|
|
|
if (null === $redirectRoute) { |
61
|
|
|
throw new NotFoundHttpException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$url = $redirectRoute->getUri(); |
65
|
|
|
|
66
|
|
|
if (!$url) { |
67
|
|
|
$routeTarget = $redirectRoute->getRouteTarget(); |
68
|
|
|
if (null !== $routeTarget) { |
69
|
|
|
$url = $this->router->generate($routeTarget, $redirectRoute->getParameters(), true); |
|
|
|
|
70
|
|
|
} else { |
71
|
|
|
$url = $this->router->generate($redirectRoute->getRouteName(), $redirectRoute->getParameters(), true); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$status = $redirectRoute->isPermanent() ? 301 : 302; |
76
|
|
|
|
77
|
|
|
return new RedirectResponse($url, $status); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: