Skip to content

Commit 8a21440

Browse files
committed
separate default controller that can be configured separately from the generic controller
1 parent 0dcc7d8 commit 8a21440

File tree

5 files changed

+106
-32
lines changed

5 files changed

+106
-32
lines changed

CHANGELOG.md

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
Changelog
22
=========
33

4+
1.1.0
5+
-----
6+
7+
* **2013-10-14**: The route enhancers now have a non-default priority so that
8+
the order is defined and you can add your own enhancers.
9+
410
1.1.0-RC8
511
---------
612

7-
* **2013-10-08**: The idPrefix is now used as a filter in getRouteByName() and getRoutesByNames()
8-
in the PHPCR RouteProvider. This means its no longer possible to get routes that are not children
9-
of a path that begins with idPrefix
13+
* **2013-10-08**: The idPrefix is now used as a filter in getRouteByName() and
14+
getRoutesByNames() in the PHPCR RouteProvider. This means its no longer
15+
possible to get routes that are not children of a path that begins with
16+
idPrefix.
1017

1118
1.1.0-RC5
1219
---------
1320

14-
* **2013-09-04**: If the route matched a pattern with a format extension, the format
15-
extension is no longer set as route a default
16-
* **2013-09-04**: Marked ContentAwareGenerator as obsolete, use ContentAwareGenerator
17-
from the CMF routing component directly instead. This class will be removed in 1.2
21+
* **2013-09-04**: If the route matched a pattern with a format extension, the
22+
format extension is no longer set as route a default.
23+
* **2013-09-04**: Marked ContentAwareGenerator as obsolete, use
24+
ContentAwareGenerator from the CMF routing component directly instead. This
25+
class will be removed in 1.2.
1826
* **2013-08-09**: dynamic.generic_controller is now defaulting to null instead
1927
of the controller from CmfContentBundle. CmfCoreBundle is prepending the
2028
CmfContentBundle generic controller if that bundle is present. If you do not
@@ -34,19 +42,29 @@ Changelog
3442
1.1.0-RC1
3543
---------
3644

37-
* **2013-07-31**: [EventDispatcher] Added events to the dynamic router at the start of match and matchRequest
38-
* **2013-07-29**: [DependencyInjection] restructured `phpcr_provider` config into `persistence` -> `phpcr` to match other Bundles
39-
* **2013-07-28**: [DependencyInjection] added `enabled` flag to `phpcr_provider` config
40-
* **2013-07-26**: [Model] Removed setRouteContent and getRouteContent, use setContent and getContent instead.
45+
* **2013-07-31**: [EventDispatcher] Added events to the dynamic router at the
46+
start of match and matchRequest.
47+
* **2013-07-29**: [DependencyInjection] restructured `phpcr_provider` config
48+
into `persistence` -> `phpcr` to match other Bundles.
49+
* **2013-07-28**: [DependencyInjection] added `enabled` flag to
50+
`phpcr_provider` config.
51+
* **2013-07-26**: [Model] Removed setRouteContent and getRouteContent, use
52+
setContent and getContent instead.
4153
* **2013-07-19**: [Model] Separated database agnostic, doctrine generic and
4254
PHPCR-ODM specific code to prepare for Doctrine ORM support.
43-
* **2013-07-17**: [FormType] Moved TermsFormType to CoreBundle and renamed it to CheckboxUrlLableFormType
55+
* **2013-07-17**: [FormType] Moved TermsFormType to CoreBundle and renamed it
56+
to CheckboxUrlLableFormType.
4457

4558
1.1.0-beta2
4659
-----------
4760

48-
* **2013-05-28**: [Bundle] Only include Doctrine PHPCR compiler pass if PHPCR-ODM is present
49-
* **2013-05-25**: [Bundle] Drop symfony_ from symfony_cmf prefix
50-
* **2013-05-24**: [Document] ContentRepository now requires ManagerRegistry in the constructor and provides `setManagerName()` in the same way as RouteProvider
51-
* **2013-05-24**: [Document] RouteProvider now requires ManagerRegistry in the constructor (the `cmf_routing.default_route_provider` does this for you)
52-
* To use a different document manager from the registry, call `setManagerName()` on the RouteProvider
61+
* **2013-05-28**: [Bundle] Only include Doctrine PHPCR compiler pass if
62+
PHPCR-ODM is present.
63+
* **2013-05-25**: [Bundle] Drop symfony_ from symfony_cmf prefix.
64+
* **2013-05-24**: [Document] ContentRepository now requires ManagerRegistry in
65+
the constructor and provides `setManagerName()` in the same way as
66+
RouteProvider.
67+
* **2013-05-24**: [Document] RouteProvider now requires ManagerRegistry in the
68+
constructor (the `cmf_routing.default_route_provider` does this for you). To
69+
use a different document manager from the registry, call `setManagerName()`
70+
on the RouteProvider.

DependencyInjection/CmfRoutingExtension.php

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
8282
}, $config[$option]);
8383
}
8484

85+
$defaultController = $config['default_controller'];
86+
if (null === $defaultController) {
87+
$defaultController = $config['generic_controller'];
88+
}
89+
$container->setParameter($this->getAlias() . '.default_controller', $defaultController);
8590
$container->setParameter($this->getAlias() . '.generic_controller', $config['generic_controller']);
8691
$container->setParameter($this->getAlias() . '.controllers_by_type', $config['controllers_by_type']);
8792
$container->setParameter($this->getAlias() . '.controllers_by_class', $config['controllers_by_class']);
@@ -135,26 +140,40 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
135140
$dynamic = $container->getDefinition($this->getAlias() . '.dynamic_router');
136141

137142
// if any mappings are defined, set the respective route enhancer
138-
if (!empty($config['generic_controller'])) {
139-
$dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer.generic_controller')));
140-
}
141143
if (!empty($config['controllers_by_type'])) {
142-
$dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer.controllers_by_type')));
144+
$dynamic->addMethodCall(
145+
'addRouteEnhancer',
146+
array(
147+
new Reference($this->getAlias() . '.enhancer.controllers_by_type'),
148+
60
149+
)
150+
);
143151
}
144152
if (!empty($config['controllers_by_class'])) {
145-
$dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer.controllers_by_class')));
153+
$dynamic->addMethodCall(
154+
'addRouteEnhancer',
155+
array(
156+
new Reference($this->getAlias() . '.enhancer.controllers_by_class'),
157+
50
158+
)
159+
);
146160
}
147-
148161
if (!empty($config['templates_by_class'])) {
149-
$dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer.templates_by_class')));
162+
$dynamic->addMethodCall(
163+
'addRouteEnhancer',
164+
array(
165+
new Reference($this->getAlias() . '.enhancer.templates_by_class'),
166+
40
167+
)
168+
);
150169

151170
/*
152171
* The CoreBundle prepends the controller from ContentBundle if the
153172
* ContentBundle is present in the project.
154-
* If you are sure you do not need a generic router, set the field
155-
* to false to disable explicitly. But you would need something
156-
* else to determine the controller in that case, as no controller
157-
* will not be set by the default route enhancers.
173+
* If you are sure you do not need a generic controller, set the field
174+
* to false to disable this check explicitly. But you would need
175+
* something else like the default_controller to set the controller,
176+
* as no controller will be set here.
158177
*/
159178
if (null === $config['generic_controller']) {
160179
throw new InvalidConfigurationException('If you want to configure templates_by_class, you need to configure the generic_controller option.');
@@ -170,9 +189,33 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
170189
$definition = $container->getDefinition($this->getAlias() . '.enhancer.controller_for_templates_by_class');
171190
$definition->replaceArgument(2, $controllerForTemplates);
172191

173-
$dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer.controller_for_templates_by_class')));
192+
$dynamic->addMethodCall(
193+
'addRouteEnhancer',
194+
array(
195+
new Reference($this->getAlias() . '.enhancer.controller_for_templates_by_class'),
196+
30
197+
)
198+
);
174199
}
175200
}
201+
if (!empty($config['generic_controller']) && $config['generic_controller'] !== $defaultController) {
202+
$dynamic->addMethodCall(
203+
'addRouteEnhancer',
204+
array(
205+
new Reference($this->getAlias() . '.enhancer.explicit_template'),
206+
10
207+
)
208+
);
209+
}
210+
if ($defaultController) {
211+
$dynamic->addMethodCall(
212+
'addRouteEnhancer',
213+
array(
214+
new Reference($this->getAlias() . '.enhancer.default_controller'),
215+
-100
216+
)
217+
);
218+
}
176219

177220
if (!empty($config['route_filters_by_id'])) {
178221
$matcher = $container->getDefinition($this->getAlias() . '.nested_matcher');

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function getConfigTreeBuilder()
5757
->canBeEnabled()
5858
->children()
5959
->scalarNode('generic_controller')->defaultNull()->end()
60+
->scalarNode('default_controller')->defaultNull()->end()
6061
->arrayNode('controllers_by_type')
6162
->useAttributeAsKey('type')
6263
->prototype('scalar')->end()

Resources/config/routing-dynamic.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<parameter key="cmf_routing.url_matcher.class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher</parameter>
1313
<parameter key="cmf_routing.generator.class">Symfony\Cmf\Component\Routing\ContentAwareGenerator</parameter>
1414
<parameter key="cmf_routing.enhancer.route_content.class">Symfony\Cmf\Component\Routing\Enhancer\RouteContentEnhancer</parameter>
15-
<parameter key="cmf_routing.enhancer.generic_controller.class">Symfony\Cmf\Component\Routing\Enhancer\FieldPresenceEnhancer</parameter>
15+
<parameter key="cmf_routing.enhancer.default_controller.class">Symfony\Cmf\Component\Routing\Enhancer\FieldPresenceEnhancer</parameter>
16+
<parameter key="cmf_routing.enhancer.explicit_template.class">Symfony\Cmf\Component\Routing\Enhancer\FieldPresenceEnhancer</parameter>
1617
<parameter key="cmf_routing.enhancer.controllers_by_type.class">Symfony\Cmf\Component\Routing\Enhancer\FieldMapEnhancer</parameter>
1718
<parameter key="cmf_routing.enhancer.field_by_class.class">Symfony\Cmf\Component\Routing\Enhancer\FieldByClassEnhancer</parameter>
1819
<parameter key="cmf_routing.redirect_controller.class">Symfony\Cmf\Bundle\RoutingBundle\Controller\RedirectController</parameter>
@@ -24,9 +25,15 @@
2425
<argument>_content</argument>
2526
</service>
2627

27-
<service id="cmf_routing.enhancer.generic_controller" class="%cmf_routing.enhancer.generic_controller.class%" public="false">
28+
<service id="cmf_routing.enhancer.default_controller" class="%cmf_routing.enhancer.default_controller.class%" public="false">
2829
<argument>null</argument>
2930
<argument>_controller</argument>
31+
<argument>%cmf_routing.default_controller%</argument>
32+
</service>
33+
34+
<service id="cmf_routing.enhancer.explicit_template" class="%cmf_routing.enhancer.explicit_template.class%" public="false">
35+
<argument>_template</argument>
36+
<argument>_controller</argument>
3037
<argument>%cmf_routing.generic_controller%</argument>
3138
</service>
3239

@@ -61,7 +68,10 @@
6168
<argument>%cmf_routing.uri_filter_regexp%</argument>
6269
<argument type="service" id="event_dispatcher" on-invalid="ignore"/>
6370
<call method="setContainer"><argument type="service" id="service_container"/></call>
64-
<call method="addRouteEnhancer"><argument type="service" id="cmf_routing.enhancer.route_content"/></call>
71+
<call method="addRouteEnhancer">
72+
<argument type="service" id="cmf_routing.enhancer.route_content"/>
73+
<argument>100</argument>
74+
</call>
6575
</service>
6676

6777
<service id="cmf_routing.nested_matcher" class="%cmf_routing.nested_matcher.class%">

Tests/Functional/Routing/DynamicRouterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public function testMatchLocale()
217217
$this->getDm()->persist($route);
218218
$childroute = new Route;
219219
$childroute->setPosition($route, 'testroute');
220+
$childroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
220221
$this->getDm()->persist($childroute);
221222
$nolocale = new Route;
222223
$nolocale->setPosition($this->getDm()->find(null, self::ROUTE_ROOT), 'es');
@@ -234,6 +235,7 @@ public function testMatchLocale()
234235
$this->router->match('/de')
235236
);
236237
$expected = array(
238+
'_controller' => 'testController',
237239
'_locale' => 'de',
238240
'_route' => self::ROUTE_ROOT . '/de/testroute'
239241
);

0 commit comments

Comments
 (0)