Skip to content

Commit

Permalink
feature #12022 [HttpKernel] Extract method to instantiate controller …
Browse files Browse the repository at this point in the history
…in ControllerResolver (danharper)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[HttpKernel] Extract method to instantiate controller in ControllerResolver

Replaces #10814 to merge into `master` instead of `2.3`.

---

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Currently it's required to duplicate the entirety of the `getController()` and `createController()` methods just to replace the call to `new` (e.g. with container resolution, instead).

Now it's possible to just override the `instantiateController()` method.

Commits
-------

88274df [HttpKernel] Extract method to make callable controller in ControllerResolver
  • Loading branch information
fabpot committed Sep 25, 2014
2 parents 11f0cb1 + 88274df commit 1104112
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
Expand Up @@ -71,7 +71,7 @@ public function getController(Request $request)

if (false === strpos($controller, ':')) {
if (method_exists($controller, '__invoke')) {
return new $controller();
return $this->instantiateController($controller);
} elseif (function_exists($controller)) {
return $controller;
}
Expand Down Expand Up @@ -153,6 +153,18 @@ protected function createController($controller)
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}

return array(new $class(), $method);
return array($this->instantiateController($class), $method);
}

/**
* Returns an instantiated controller
*
* @param string $class A class name
*
* @return object
*/
protected function instantiateController($class)
{
return new $class();
}
}

0 comments on commit 1104112

Please sign in to comment.