Skip to content

Commit

Permalink
support controller as a service in RouteCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Dec 18, 2011
1 parent ec093da commit d7af08a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
14 changes: 13 additions & 1 deletion Resources/doc/reference/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ reduce overhead.
Declaring a new Admin class
---------------------------

Once you have created an admin class, you must declare the class to use it. Like
Once you have created an admin class, you need to make the framework aware of
it. To do that, you need to add a tag with the name ``sonata.admin`` to the
service. Parameters for that tag are:
* ``manager_type``: Label of the document manager to inject;
* ``group``: A label to allow grouping on the dashboard;
* ``label``: Label to use for the name of the entity this manager handles;

Examples:

.. code-block:: xml
Expand All @@ -138,4 +145,9 @@ Or if you're using a YML configuration file,
arguments: [null, Sonata\NewsBundle\Entity\Post, SonataNewsBundle:PostAdmin]
You can extend ``Sonata\AdminBundle\Admin\Admin`` to minimize the amount of
code to write. This base admin uses the routing services to build routes.
Note that you can use both the Bundle:Controller format or a service name to
specify what controller to load.

.. _`Django Project Website`: http://www.djangoproject.com/
6 changes: 6 additions & 0 deletions Route/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public function actionify($action)
$action = substr($action, $pos + 1);
}

// if this is a service rather than just a controller name, the suffix
// Action is not automatically appended to the method name
if (strpos($this->baseControllerName, ':') === false) {
$action .= 'Action';
}

return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
}

Expand Down
27 changes: 24 additions & 3 deletions Tests/Route/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ public function testGetter()

public function testActionify()
{
$routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
$routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'BundleName:ControllerName');

$this->assertEquals('fooBar', $routeCollection->actionify('Foo bar'));
$this->assertEquals('bar', $routeCollection->actionify('Foo.bar'));
}

public function testActionifyService()
{
$routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerService');

$this->assertEquals('fooBarAction', $routeCollection->actionify('Foo bar'));
$this->assertEquals('barAction', $routeCollection->actionify('Foo.bar'));
}

public function testCode()
{
$routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
Expand Down Expand Up @@ -78,13 +86,26 @@ public function testChildCollection()

public function testRoute()
{
$routeCollection = new RouteCollection('baseCodeRoute', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
$routeCollection = new RouteCollection('baseCodeRoute', 'baseRouteName', 'baseRoutePattern', 'BundleName:ControllerName');

$routeCollection->add('view');

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

$this->assertEquals('BundleName:ControllerName:view', $route->getDefault('_controller'));
$this->assertEquals('baseCodeRoute', $route->getDefault('_sonata_admin'));
$this->assertEquals('baseRouteName_view', $route->getDefault('_sonata_name'));
}

public function testRouteControllerService()
{
$routeCollection = new RouteCollection('baseCodeRoute', 'baseRouteName', 'baseRoutePattern', 'baseControllerServiceName');

$routeCollection->add('view');

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

$this->assertEquals('baseControllerName:view', $route->getDefault('_controller'));
$this->assertEquals('baseControllerServiceName:viewAction', $route->getDefault('_controller'));
$this->assertEquals('baseCodeRoute', $route->getDefault('_sonata_admin'));
$this->assertEquals('baseRouteName_view', $route->getDefault('_sonata_name'));
}
Expand Down

0 comments on commit d7af08a

Please sign in to comment.