Commit 221ea9bd authored by Igor Wiedler's avatar Igor Wiedler

fix documentation on callable controllers (thanks Bart)

parent 5b1a28cf
...@@ -60,15 +60,7 @@ A route pattern consists of: ...@@ -60,15 +60,7 @@ A route pattern consists of:
only ``GET`` and ``POST`` are used, but it is possible to use the only ``GET`` and ``POST`` are used, but it is possible to use the
others as well. others as well.
The controller can be any PHP callable, so either of:: The controller is defined using a closure like this::
'functionName'
array('Class', 'staticMethodName')
array($object, 'methodName')
But the encouraged way of defining controllers is a closure is this::
function() { function() {
// do something // do something
...@@ -87,6 +79,10 @@ closure in a function and import local variables of that function. ...@@ -87,6 +79,10 @@ closure in a function and import local variables of that function.
The return value of the closure becomes the content of the page. The return value of the closure becomes the content of the page.
There is also an alternate way for defining controllers using a
class method. The syntax for that is ``ClassName::methodName``.
Static methods are also possible.
Example GET route Example GET route
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
......
...@@ -14,7 +14,7 @@ namespace Silex\Tests; ...@@ -14,7 +14,7 @@ namespace Silex\Tests;
use Silex\Application; use Silex\Application;
/** /**
* Controller test cases. * Functional test cases.
* *
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
*/ */
......
...@@ -173,6 +173,24 @@ class RouterTest extends \PHPUnit_Framework_TestCase ...@@ -173,6 +173,24 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/foo/', $response->headers->get('Location')); $this->assertEquals('/foo/', $response->headers->get('Location'));
} }
public function testClassNameControllerSyntax()
{
$app = new Application();
$app->get('/foo', 'Silex\Tests\MyController::getFoo');
$this->checkRouteResponse($app, '/foo', 'foo');
}
public function testClassNameControllerSyntaxWithStaticMethod()
{
$app = new Application();
$app->get('/bar', 'Silex\Tests\MyController::getBar');
$this->checkRouteResponse($app, '/bar', 'bar');
}
protected function checkRouteResponse($app, $path, $expectedContent, $method = 'get', $message = null) protected function checkRouteResponse($app, $path, $expectedContent, $method = 'get', $message = null)
{ {
$request = Request::create($path, $method); $request = Request::create($path, $method);
...@@ -180,3 +198,16 @@ class RouterTest extends \PHPUnit_Framework_TestCase ...@@ -180,3 +198,16 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expectedContent, $response->getContent(), $message); $this->assertEquals($expectedContent, $response->getContent(), $message);
} }
} }
class MyController
{
public function getFoo()
{
return 'foo';
}
static public function getBar()
{
return 'bar';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment