From 28e32d480f7ad155243aeff6c41711507941557c Mon Sep 17 00:00:00 2001 From: Smaine Milianni Date: Sun, 28 Jan 2018 19:31:14 +0100 Subject: [PATCH] Add the possibility to prefix URLs and routes name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I saw this issue https://github.com/symfony/symfony-docs/issues/8358 and I want to solve it but I am not sure if it's the good place to add this information 😀 --- routing.rst | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/routing.rst b/routing.rst index 54576f75ea1..b9210122817 100644 --- a/routing.rst +++ b/routing.rst @@ -126,6 +126,8 @@ Whenever you have a ``{placeholder}`` in your route path, that portion becomes a wildcard: it matches *any* value. Your controller can now *also* have an argument called ``$placeholder`` (the wildcard and argument names *must* match). + + Each route also has an internal name: ``blog_list`` and ``blog_show``. These can be anything (as long as each is unique) and don't have any meaning yet. You'll use them later to :ref:`generate URLs `. @@ -241,6 +243,35 @@ URL Route Parameters To learn about other route requirements - like HTTP method, hostname and dynamic expressions - see :doc:`/routing/requirements`. +Prefix all controller route names and URLs +------------------------------------------ + +You have the possibility to prefix all routes names and URLs used by the action methods of a controller with the ``@Route`` annotation. + +Add a name property to the ``@Route`` annotation of the controller class and that will be considered the prefix of all route names + +.. code-block:: php + + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + //prefix all URLs of this controller's action method with /blog and routes name with blog_ + /** + * @Route("/blog", name="blog_") + */ + class BlogController extends Controller + { + /** + * @Route("/", defaults={"page": "1"}, name="index") + */ + public function indexAction($page, $_format) { ... } + + /** + * @Route("/posts/{slug}", name="post") + */ + public function showAction(Post $post) { ... } + } + +In this example, the URL of the index action will be ``/blog/`` and ``/blog/posts/``... and the URL of the show action will be /blog/posts/... It's the same logic for the routes name that will be ``blog_index`` and ``blog_post``. + Giving {placeholders} a Default Value -------------------------------------