Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
221ea9bd
Commit
221ea9bd
authored
Apr 14, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix documentation on callable controllers (thanks Bart)
parent
5b1a28cf
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
10 deletions
+37
-10
doc/usage.rst
doc/usage.rst
+5
-9
tests/Silex/Tests/FunctionalTest.php
tests/Silex/Tests/FunctionalTest.php
+1
-1
tests/Silex/Tests/RouterTest.php
tests/Silex/Tests/RouterTest.php
+31
-0
No files found.
doc/usage.rst
View file @
221ea9bd
...
...
@@ -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
~~~~~~~~~~~~~~~~~
...
...
tests/Silex/Tests/FunctionalTest.php
View file @
221ea9bd
...
...
@@ -14,7 +14,7 @@ namespace Silex\Tests;
use
Silex\Application
;
/**
*
Controller
test cases.
*
Functional
test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
...
...
tests/Silex/Tests/RouterTest.php
View file @
221ea9bd
...
...
@@ -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'
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment