Commit 605f8262 authored by Fabien Potencier's avatar Fabien Potencier

merged branch igorw/controllers-in-classes (PR #578)

This PR was merged into the master branch.

Commits
-------

2904a7a9 [docs] Inject $app to showcase that possibility
8ac1c044 [docs] Add a section about controllers in classes to usage doc

Discussion
----------

[docs] Add a section about controllers in classes to usage doc

---------------------------------------------------------------------------

by ChrisRiddell at 2013-01-10T23:40:43Z

👍 The little more information this gives is helpful :)
parents 28e67736 2904a7a9
......@@ -123,10 +123,6 @@ 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
~~~~~~~~~~~~~~~~~
......@@ -383,6 +379,36 @@ really be used. You can give a route a name by calling ``bind`` on the
It only makes sense to name routes if you use providers that make use of
the ``RouteCollection``.
Controllers in classes
~~~~~~~~~~~~~~~~~~~~~~
If you don't want to use anonymous functions, you can also define your
controllers as methods. By using the ``ControllerClass::methodName`` syntax,
you can tell Silex to lazily create that controller class for you::
$app->get('/', 'Igorw\Foo::bar');
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
namespace Igorw
{
class Foo
{
public function bar(Request $request, Application $app)
{
...
}
}
}
This will load the ``Igorw\Foo`` class on demand, create an instance and call
the ``bar`` method to get the response. You can use ``Request`` and
``Silex\Application`` type hints to get ``$request`` and ``$app`` injected.
For an even stronger separation between Silex and your controllers, you can
:doc:`define your controllers as services <providers/service_controller>`.
Global Configuration
--------------------
......
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