Skip to content
Next Next commit
Renamed AcmeHelloBundle to AppBundle
  • Loading branch information
wouterj committed Nov 5, 2014
commit 87b324a1237ac19d9f2504375d187e6cfce3b460
59 changes: 29 additions & 30 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ or a ``Closure``), in Symfony, a controller is usually a single method inside
a controller object. Controllers are also called *actions*.

.. code-block:: php
:linenos:

// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
// src/AppBundle/Controller/HelloController.php
namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -151,7 +150,7 @@ to the controller:
# app/config/routing.yml
hello:
path: /hello/{name}
defaults: { _controller: AcmeHelloBundle:Hello:index }
defaults: { _controller: AppBundle:Hello:index }

.. code-block:: xml

Expand All @@ -163,7 +162,7 @@ to the controller:
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="hello" path="/hello/{name}">
<default key="_controller">AcmeHelloBundle:Hello:index</default>
<default key="_controller">AppBundle:Hello:index</default>
</route>
</routes>

Expand All @@ -175,7 +174,7 @@ to the controller:

$collection = new RouteCollection();
$collection->add('hello', new Route('/hello/{name}', array(
'_controller' => 'AcmeHelloBundle:Hello:index',
'_controller' => 'AppBundle:Hello:index',
)));

return $collection;
Expand All @@ -184,10 +183,10 @@ Going to ``/hello/ryan`` now executes the ``HelloController::indexAction()``
controller and passes in ``ryan`` for the ``$name`` variable. Creating a
"page" means simply creating a controller method and associated route.

Notice the syntax used to refer to the controller: ``AcmeHelloBundle:Hello:index``.
Notice the syntax used to refer to the controller: ``AppBundle:Hello:index``.
Symfony uses a flexible string notation to refer to different controllers.
This is the most common syntax and tells Symfony to look for a controller
class called ``HelloController`` inside a bundle named ``AcmeHelloBundle``. The
class called ``HelloController`` inside a bundle named ``AppBundle``. The
method ``indexAction()`` is then executed.

For more details on the string format used to reference different controllers,
Expand All @@ -212,13 +211,13 @@ see :ref:`controller-string-syntax`.
Route Parameters as Controller Arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You already know that the ``_controller`` parameter ``AcmeHelloBundle:Hello:index``
You already know that the ``_controller`` parameter ``AppBundle:Hello:index``
refers to a ``HelloController::indexAction()`` method that lives inside the
``AcmeHelloBundle`` bundle. What's more interesting is the arguments that are
passed to that method::
``AppBundle`` bundle. What's more interesting is the arguments that are passed
to that method::

// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
// src/AppBundle/Controller/HelloController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

Expand All @@ -243,7 +242,7 @@ example:
# app/config/routing.yml
hello:
path: /hello/{firstName}/{lastName}
defaults: { _controller: AcmeHelloBundle:Hello:index, color: green }
defaults: { _controller: AppBundle:Hello:index, color: green }

.. code-block:: xml

Expand All @@ -255,7 +254,7 @@ example:
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="hello" path="/hello/{firstName}/{lastName}">
<default key="_controller">AcmeHelloBundle:Hello:index</default>
<default key="_controller">AppBundle:Hello:index</default>
<default key="color">green</default>
</route>
</routes>
Expand All @@ -268,7 +267,7 @@ example:

$collection = new RouteCollection();
$collection->add('hello', new Route('/hello/{firstName}/{lastName}', array(
'_controller' => 'AcmeHelloBundle:Hello:index',
'_controller' => 'AppBundle:Hello:index',
'color' => 'green',
)));

Expand Down Expand Up @@ -377,8 +376,8 @@ you can take advantage of several helper methods.
Add the ``use`` statement atop the ``Controller`` class and then modify the
``HelloController`` to extend it::

// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
// src/AppBundle/Controller/HelloController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -472,7 +471,7 @@ object that's returned from that controller::

public function indexAction($name)
{
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
$response = $this->forward('AppBundle:Something:fancy', array(
'name' => $name,
'color' => 'green',
));
Expand All @@ -484,22 +483,22 @@ object that's returned from that controller::

Notice that the ``forward()`` method uses the same string representation of
the controller used in the routing configuration. In this case, the target
controller class will be ``HelloController`` inside some ``AcmeHelloBundle``.
The array passed to the method becomes the arguments on the resulting controller.
This same interface is used when embedding controllers into templates (see
:ref:`templating-embedding-controller`). The target controller method should
look something like the following::
controller class will be ``SomethingController::fancyAction()`` inside the
``AppBundle``. The array passed to the method becomes the arguments on the
resulting controller. This same interface is used when embedding controllers
into templates (see :ref:`templating-embedding-controller`). The target
controller method should look something like the following::

public function fancyAction($name, $color)
{
// ... create and return a Response object
}

And just like when creating a controller for a route, the order of the arguments
to ``fancyAction`` doesn't matter. Symfony matches the index key names
(e.g. ``name``) with the method argument names (e.g. ``$name``). If you
change the order of the arguments, Symfony will still pass the correct
value to each variable.
Just like when creating a controller for a route, the order of the arguments of
``fancyAction`` doesn't matter. Symfony matches the index key names (e.g.
``name``) with the method argument names (e.g. ``$name``). If you change the
order of the arguments, Symfony will still pass the correct value to each
variable.

.. tip::

Expand All @@ -512,7 +511,7 @@ value to each variable.
use Symfony\Component\HttpKernel\HttpKernelInterface;

$path = array(
'_controller' => 'AcmeHelloBundle:Hello:fancy',
'_controller' => 'AppBundle:Something:fancy',
'name' => $name,
'color' => 'green',
);
Expand Down