<commit_before>View Response listener
======================

The view response listener makes it possible to simply return a ``View``
instance from action controllers. The final output will then automatically be
processed via the listener by the ``fos_rest.view_handler`` service.

This requires adding the `SensioFrameworkExtraBundle`_ to your vendors.

Now inside a controller it's possible to simply return a ``View`` instance.

.. code-block:: php

    <?php

    use FOS\RestBundle\View\View;

    class UsersController
    {
        public function getUsersAction()
        {
            $view = View::create();

            // ...

            $view->setData($data);
            return $view;
        }
    }

As this feature is heavily based on the `SensioFrameworkExtraBundle`_, the
example can further be simplified by using the various annotations supported by
that bundle. There is also one additional annotation called ``@View()`` which
extends from the ``@Template()`` annotation.

The ``@View()`` and ``@Template()`` annotations behave essentially the same with
a minor difference. When ``view_response_listener`` is set to ``true`` instead
of ``force`` and ``@View()`` is not used, then rendering will be delegated to
`SensioFrameworkExtraBundle`_.

Note that it is necessary to disable view annotations in
`SensioFrameworkExtraBundle`_ so that FOSRestBundle can take over the handling.
However, FOSRestBundle will do this automatically but it does not override any
explicit configuration. So make sure to remove or disable the following setting:

.. code-block:: yaml

    # app/config/config.yml
    fos_rest:
        view:
            view_response_listener: force

    sensio_framework_extra:
        view:    { annotations: false }

.. code-block:: php

    <?php

    use FOS\RestBundle\Controller\Annotations\View;

    class UsersController
    {
        /**
         * @View()
         */
        public function getUsersAction()
        {
            // ...

            return $data;
        }
    }

If you need to pass additional data to template, not for serialization,
you should return a $view object with the data set by ``setTemplateData``.

.. code-block:: php
<commit_msg>text fixes suggested by @xabbuh<commit_after>View Response listener
======================

The view response listener makes it possible to simply return a ``View``
instance from action controllers. The final output will then automatically be
processed via the listener by the ``fos_rest.view_handler`` service.

This requires adding the `SensioFrameworkExtraBundle`_ to your vendors.

Now inside a controller it's possible to simply return a ``View`` instance.

.. code-block:: php

    <?php

    use FOS\RestBundle\View\View;

    class UsersController
    {
        public function getUsersAction()
        {
            $view = View::create();

            // ...

            $view->setData($data);
            return $view;
        }
    }

As this feature is heavily based on the `SensioFrameworkExtraBundle`_, the
example can further be simplified by using the various annotations supported by
that bundle. There is also one additional annotation called ``@View()`` which
extends from the ``@Template()`` annotation.

The ``@View()`` and ``@Template()`` annotations behave essentially the same with
a minor difference. When ``view_response_listener`` is set to ``true`` instead
of ``force`` and ``@View()`` is not used, then rendering will be delegated to
`SensioFrameworkExtraBundle`_.

Note that it is necessary to disable view annotations in
`SensioFrameworkExtraBundle`_ so that FOSRestBundle can take over the handling.
However, FOSRestBundle will do this automatically but it does not override any
explicit configuration. So make sure to remove or disable the following setting:

.. code-block:: yaml

    # app/config/config.yml
    fos_rest:
        view:
            view_response_listener: force

    sensio_framework_extra:
        view:    { annotations: false }

.. code-block:: php

    <?php

    use FOS\RestBundle\Controller\Annotations\View;

    class UsersController
    {
        /**
         * @View()
         */
        public function getUsersAction()
        {
            // ...

            return $data;
        }
    }

If you need to pass additional data to the template, not for serialization,
you should return a ``$view`` object with the data set by ``setTemplateData``.

.. code-block:: php

{'fraction_non_alphanumeric': 0.09563456345634563, 'fraction_numerical': 0.0, 'mean_word_length': 2.975849731663685, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2125557', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1120, 'n_words': 473}