Prechádzať zdrojové kódy

[FrameworkBundle] added support for services as controllers

Fabien Potencier 14 rokov pred
rodič
commit
2d80788e3a

+ 12 - 2
src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php

@@ -57,8 +57,18 @@ class ControllerResolver extends BaseControllerResolver
     protected function createController($controller)
     protected function createController($controller)
     {
     {
         if (false === strpos($controller, '::')) {
         if (false === strpos($controller, '::')) {
-            // must be a controller in the a:b:c notation then
-            $controller = $this->converter->fromShortNotation($controller);
+            $count = substr_count($controller, ':');
+            if (2 == $count) {
+                // controller in the a:b:c notation then
+                $controller = $this->converter->fromShortNotation($controller);
+            } elseif (1 == $count) {
+                // controller in the service:method notation
+                list($service, $method) = explode(':', $controller);
+
+                return array($this->container->get($service), $method);
+            } else {
+                throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
+            }
         }
         }
 
 
         list($class, $method) = explode('::', $controller);
         list($class, $method) = explode('::', $controller);

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php

@@ -60,7 +60,7 @@ class DelegatingLoader extends BaseDelegatingLoader
                 try {
                 try {
                     $controller = $this->converter->fromShortNotation($controller);
                     $controller = $this->converter->fromShortNotation($controller);
                 } catch (\Exception $e) {
                 } catch (\Exception $e) {
-                    throw new \RuntimeException(sprintf('%s (for route "%s" in resource "%s")', $e->getMessage(), $name, is_string($resource) ? $resource : 'RESOURCE'), $e->getCode(), $e);
+                    // unable to optimize unknown notation
                 }
                 }
 
 
                 $route->setDefault('_controller', $controller);
                 $route->setDefault('_controller', $controller);