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:
only ``GET`` and ``POST`` are used, but it is possible to use the
others as well.
The controller can be any PHP callable, so either of::
'functionName'
array('Class', 'staticMethodName')
array($object, 'methodName')
But the encouraged way of defining controllers is a closure is this::
The controller is defined using a closure like this::
function() {
// do something
......@@ -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.
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
~~~~~~~~~~~~~~~~~
......
......@@ -14,7 +14,7 @@ namespace Silex\Tests;
use Silex\Application;
/**
* Controller test cases.
* Functional test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
......
......@@ -173,6 +173,24 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$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)
{
$request = Request::create($path, $method);
......@@ -180,3 +198,16 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$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