Skip to content

Add support for "controller" keyword for configuring routes controllers #23227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Added support for prioritized routing loaders.
* Add matched and default parameters to redirect responses
* Added support for a `controller` keyword for configuring route controllers in YAML and XML configurations.

3.3.0
-----
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ private function parseConfigs(\DOMElement $node, $path)
}
}

if ($controller = $node->getAttribute('controller')) {
if (isset($defaults['_controller'])) {
$name = $node->hasAttribute('id') ? sprintf('"%s"', $node->getAttribute('id')) : sprintf('the "%s" tag', $node->tagName);

throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" attribute and the defaults key "_controller" for %s.', $path, $name));
}

$defaults['_controller'] = $controller;
}

return array($defaults, $requirements, $options, $condition);
}

Expand Down
13 changes: 12 additions & 1 deletion src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class YamlFileLoader extends FileLoader
{
private static $availableKeys = array(
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
);
private $yamlParser;

Expand Down Expand Up @@ -115,6 +115,10 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
$methods = isset($config['methods']) ? $config['methods'] : array();
$condition = isset($config['condition']) ? $config['condition'] : null;

if (isset($config['controller'])) {
$defaults['_controller'] = $config['controller'];
}

$route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);

$collection->add($name, $route);
Expand All @@ -140,6 +144,10 @@ protected function parseImport(RouteCollection $collection, array $config, $path
$schemes = isset($config['schemes']) ? $config['schemes'] : null;
$methods = isset($config['methods']) ? $config['methods'] : null;

if (isset($config['controller'])) {
$defaults['_controller'] = $config['controller'];
}

$this->setCurrentDir(dirname($path));

$subCollection = $this->import($config['resource'], $type, false, $file);
Expand Down Expand Up @@ -203,5 +211,8 @@ protected function validate($config, $name, $path)
$name, $path
));
}
if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
Copy link
Contributor

@ro0NL ro0NL Jul 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be written as isset($a, $b)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly you can't. See #22684.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, i've forgot about that. 👍

throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
<xsd:attribute name="controller" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="import">
Expand All @@ -52,6 +53,7 @@
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
<xsd:attribute name="controller" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="default" mixed="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="routing.xml">
<default key="_controller">FrameworkBundle:Template:template</default>
</import>
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_static:
resource: routing.yml
defaults:
_controller: FrameworkBundle:Template:template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="routing.xml" controller="FrameworkBundle:Template:template" />
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_static:
resource: routing.yml
controller: FrameworkBundle:Template:template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="routing.xml" controller="FrameworkBundle:Template:template">
<default key="_controller">AppBundle:Blog:index</default>
</import>
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_static:
resource: routing.yml
controller: FrameworkBundle:Template:template
defaults:
_controller: AppBundle:Homepage:show
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="app_blog" path="/blog" controller="AppBundle:Homepage:show">
<default key="_controller">AppBundle:Blog:index</default>
</route>
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app_blog:
path: /blog
controller: AppBundle:Homepage:show
defaults:
_controller: AppBundle:Blog:index
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="app_homepage" path="/" controller="AppBundle:Homepage:show" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also have another imported route that does not specify the controller attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about a case where an imported route makes use of the _controller default?


<route id="app_blog" path="/blog">
<default key="_controller">AppBundle:Blog:list</default>
</route>

<route id="app_logout" path="/logout" />
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
app_homepage:
path: /
controller: AppBundle:Homepage:show

app_blog:
path: /blog
defaults:
_controller: AppBundle:Blog:list

app_logout:
path: /logout
74 changes: 74 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,78 @@ public function testNullValuesInMap()
$route->getDefault('map')
);
}

public function testLoadRouteWithControllerAttribute()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load('routing.xml');

$route = $routeCollection->get('app_homepage');

$this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
}

public function testLoadRouteWithoutControllerAttribute()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load('routing.xml');

$route = $routeCollection->get('app_logout');

$this->assertNull($route->getDefault('_controller'));
}

public function testLoadRouteWithControllerSetInDefaults()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load('routing.xml');

$route = $routeCollection->get('app_blog');

$this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" attribute and the defaults key "_controller" for "app_blog"/
*/
public function testOverrideControllerInDefaults()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('override_defaults.xml');
}

/**
* @dataProvider provideFilesImportingRoutesWithControllers
*/
public function testImportRouteWithController($file)
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load($file);

$route = $routeCollection->get('app_homepage');
$this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));

$route = $routeCollection->get('app_blog');
$this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));

$route = $routeCollection->get('app_logout');
$this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
}

public function provideFilesImportingRoutesWithControllers()
{
yield array('import_controller.xml');
yield array('import__controller.xml');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" attribute and the defaults key "_controller" for the "import" tag/
*/
public function testImportWithOverriddenController()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('import_override_defaults.xml');
}
}
74 changes: 74 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,78 @@ public function testLoadWithResource()
$this->assertSame('context.getMethod() == "POST"', $route->getCondition());
}
}

public function testLoadRouteWithControllerAttribute()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load('routing.yml');

$route = $routeCollection->get('app_homepage');

$this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
}

public function testLoadRouteWithoutControllerAttribute()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load('routing.yml');

$route = $routeCollection->get('app_logout');

$this->assertNull($route->getDefault('_controller'));
}

public function testLoadRouteWithControllerSetInDefaults()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load('routing.yml');

$route = $routeCollection->get('app_blog');

$this->assertSame('AppBundle:Blog:list', $route->getDefault('_controller'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "app_blog"/
*/
public function testOverrideControllerInDefaults()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('override_defaults.yml');
}

/**
* @dataProvider provideFilesImportingRoutesWithControllers
*/
public function testImportRouteWithController($file)
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$routeCollection = $loader->load($file);

$route = $routeCollection->get('app_homepage');
$this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));

$route = $routeCollection->get('app_blog');
$this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));

$route = $routeCollection->get('app_logout');
$this->assertSame('FrameworkBundle:Template:template', $route->getDefault('_controller'));
}

public function provideFilesImportingRoutesWithControllers()
{
yield array('import_controller.yml');
yield array('import__controller.yml');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The routing file "[^"]*" must not specify both the "controller" key and the defaults key "_controller" for "_static"/
*/
public function testImportWithOverriddenController()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('import_override_defaults.yml');
}
}