Skip to content

Commit ec8cb97

Browse files
committed
Merge branch 'ilukac-support_redirect_with_orm'
2 parents 9e113de + ee62a4b commit ec8cb97

File tree

5 files changed

+188
-1
lines changed

5 files changed

+188
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
2.3.0 (unreleased)
5+
------------------
6+
7+
* Implemented redirect route feature for the Doctrine ORM model
8+
* Removed the name for staticPrefix index on the Route model due to name colision when the model is inherited by RediretRoute model
9+
410
2.2.0
511
-----
612

src/Doctrine/Orm/RedirectRoute.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 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\RoutingBundle\Doctrine\Orm;
13+
14+
use Symfony\Cmf\Bundle\RoutingBundle\Model\RedirectRoute as RedirectRouteModel;
15+
16+
/**
17+
* {@inheritdoc}
18+
*
19+
* Provides a redirect route stored in the Doctrine ORM and used as content for generic route to provide redirects
20+
*/
21+
class RedirectRoute extends RedirectRouteModel
22+
{
23+
/**
24+
* Unique id of this route.
25+
*
26+
* @var int
27+
*/
28+
protected $id;
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $serialisedParameters;
34+
35+
/**
36+
* Overwrite to be able to create route without pattern.
37+
*
38+
* Additional options:
39+
*
40+
* * add_trailing_slash: When set, a trailing slash is appended to the route
41+
*
42+
* @param array $options
43+
*/
44+
public function __construct(array $options = [])
45+
{
46+
parent::__construct($options);
47+
}
48+
49+
public function setId($id)
50+
{
51+
$this->id = $id;
52+
53+
return $this;
54+
}
55+
56+
public function getId()
57+
{
58+
return $this->id;
59+
}
60+
61+
/**
62+
* Set the parameters for building this route. Used with both route name
63+
* and target route document.
64+
*/
65+
public function setParameters(array $parameters)
66+
{
67+
$this->serialisedParameters = json_encode($parameters);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function getParameters()
74+
{
75+
$params = json_decode($this->serialisedParameters);
76+
77+
return is_array($params) ? $params : [];
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function getPath()
84+
{
85+
$pattern = parent::getPath();
86+
if ($this->getOption('add_trailing_slash') && '/' !== $pattern[strlen($pattern) - 1]) {
87+
$pattern .= '/';
88+
}
89+
90+
return $pattern;
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
protected function isBooleanOption($name)
97+
{
98+
return 'add_trailing_slash' === $name || parent::isBooleanOption($name);
99+
}
100+
}

src/Resources/config/doctrine-model/Route.orm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<field name="staticPrefix" type="string" nullable="true" column="staticPrefix"/>
99

1010
<indexes>
11-
<index name="prefix_idx" columns="staticPrefix"/>
11+
<index columns="staticPrefix"/>
1212
</indexes>
1313
</mapped-superclass>
1414

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
4+
5+
<entity name="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RedirectRoute" table="orm_redirects">
6+
7+
<id name="id" type="integer" column="id">
8+
<generator strategy="AUTO"/>
9+
</id>
10+
11+
<!-- we hardcode the column name to overwrite column naming strategies as we have to define the index on the column name -->
12+
<field name="routeName" type="string" unique="true" column="routeName"/>
13+
<field name="uri" type="string" nullable="true"/>
14+
<field name="permanent" type="boolean"/>
15+
16+
<many-to-one field="routeTarget" target-entity="Route" >
17+
<join-column name="routeTargetId" referenced-column-name="id" />
18+
</many-to-one>
19+
20+
</entity>
21+
22+
</doctrine-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 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\RoutingBundle\Tests\Functional\Doctrine\Orm;
13+
14+
use Symfony\Cmf\Bundle\RoutingBundle\Controller\RedirectController;
15+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RedirectRoute;
16+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
17+
use Symfony\Component\HttpFoundation\RedirectResponse;
18+
19+
class RedirectRouteTest extends OrmTestCase
20+
{
21+
private $repository;
22+
23+
private $controller;
24+
25+
public function setUp()
26+
{
27+
parent::setUp();
28+
$this->clearDb(Route::class);
29+
$this->clearDb(RedirectRoute::class);
30+
31+
$this->repository = $this->getContainer()->get('cmf_routing.route_provider');
32+
$this->controller = new RedirectController($this->getContainer()->get('router'));
33+
}
34+
35+
public function testRedirectDoctrine()
36+
{
37+
$route = $this->createRoute('route1', '/test');
38+
39+
$redirectRouteDoc = new RedirectRoute();
40+
$redirectRouteDoc->setRouteName('redirect1');
41+
$redirectRouteDoc->setRouteTarget($route);
42+
$redirectRouteDoc->setPermanent(true);
43+
44+
$this->getDm()->persist($redirectRouteDoc);
45+
$this->getDm()->flush();
46+
47+
$redirectRoute = $this->createRoute('redirect1', '/redirect');
48+
$redirectRoute->setContent($redirectRouteDoc);
49+
50+
$this->getDm()->flush();
51+
$this->getDm()->clear();
52+
53+
$response = $this->controller->redirectAction($redirectRoute->getContent());
54+
55+
$this->assertInstanceOf(RedirectResponse::class, $response);
56+
$this->assertSame(301, $response->getStatusCode());
57+
$this->assertSame('http://localhost/test', $response->getTargetUrl());
58+
}
59+
}

0 commit comments

Comments
 (0)