add support for re-use of same controller in CRUD

This commit is contained in:
Julien Fastré 2020-01-14 22:45:30 +01:00
parent 6109520c95
commit ce365b2c41
3 changed files with 8 additions and 7 deletions

View File

@ -62,10 +62,9 @@ class CRUDRoutesLoader
protected function loadConfig($config): RouteCollection
{
$collection = new RouteCollection();
foreach ($config['actions'] as $name => $action) {
$defaults = [
'_controller' => $action['controller'] ?? $config['controller'].'::'.$name
'_controller' => 'cscrud_'.$config['name'].'_controller'.':'.($action['controller_action'] ?? $name)
];
if ($name === 'index') {

View File

@ -228,6 +228,7 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
foreach ($config as $crudEntry) {
$controller = $crudEntry['controller'];
$controllerServiceName = 'cscrud_'.$crudEntry['name'].'_controller';
$name = $crudEntry['name'];
// check for existing crud names
@ -235,15 +236,16 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
throw new LogicException(sprintf("the name %s is defined twice in CRUD", $name));
}
if (!$container->has($controller)) {
if (!$container->has($controllerServiceName)) {
$controllerDefinition = new Definition($controller);
$controllerDefinition->addTag('controller.service_arguments');
$controllerDefinition->setAutoconfigured(true);
$container->setDefinition($controller, $controllerDefinition);
$controllerDefinition->setClass($crudEntry['controller']);
$container->setDefinition($controllerServiceName, $controllerDefinition);
}
$container->setParameter('chill_main_crud_config_'.$name, $crudEntry);
$container->getDefinition($controller)
$container->getDefinition($controllerServiceName)
->addMethodCall('setCrudConfig', ['%chill_main_crud_config_'.$name.'%']);
}
}

View File

@ -134,10 +134,10 @@ class Configuration implements ConfigurationInterface
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->scalarNode('controller')
->scalarNode('controller_action')
->defaultNull()
->info('the method name to call in the route. Will be set to the action name if left empty.')
->example("'MyBundle\Controller\MyCrudController::action'")
->example("'action'")
->end()
->scalarNode('path')
->defaultNull()