Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit efd0458

Browse files
committed
Merge pull request #146 from symfony-cmf/redirect_controller
Added redirect controller
2 parents 0347e26 + c8f1d6c commit efd0458

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed

Controller/RedirectController.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
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 Symfony\Cmf\Bundle\RoutingAutoBundle\Controller;
13+
14+
use Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface;
15+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\HttpFoundation\RedirectResponse;
17+
use Symfony\Component\Routing\RouterInterface;
18+
19+
/**
20+
* Simple redirecting controller for AutoRouteInterface documents
21+
*
22+
* @author Daniel Leech <[email protected]>
23+
*/
24+
class RedirectController
25+
{
26+
/**
27+
* @var RouterInterface
28+
*/
29+
protected $router;
30+
31+
/**
32+
* @param RouterInterface $router
33+
*/
34+
public function __construct(RouterInterface $router)
35+
{
36+
$this->router = $router;
37+
}
38+
39+
/**
40+
* @param AutoRouteInterface $routeDocument
41+
*/
42+
public function redirectAction(AutoRouteInterface $routeDocument)
43+
{
44+
$routeTarget = $routeDocument->getRedirectTarget();
45+
$url = $this->router->generate($routeTarget);
46+
47+
return new RedirectResponse($url, 302);
48+
}
49+
}

Resources/config/auto_route.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@
8686
class="%cmf_routing_auto.metadata.factory.class%"
8787
factory-service="cmf_routing_auto.metadata.factory.builder"
8888
factory-method="getMetadataFactory"
89-
/>
89+
/>
90+
91+
<!-- Controller -->
92+
<service
93+
id="cmf_routing_auto.redirect_controller"
94+
class="Symfony\Cmf\Bundle\RoutingAutoBundle\Controller\RedirectController"
95+
>
96+
<argument type="service" id="router" />
97+
</service>
9098
</services>
9199
</container>

Tests/Resources/Controller/TestController.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

Tests/Resources/app/config/app_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cmf_routing:
66
dynamic:
77
enabled: true
88
controllers_by_type:
9-
cmf_routing_auto.redirect: Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Controller\TestController::redirectAction
9+
cmf_routing_auto.redirect: cmf_routing_auto.redirect_controller:redirectAction
1010
persistence:
1111
phpcr:
1212
enabled: true

0 commit comments

Comments
 (0)