From 87b324a1237ac19d9f2504375d187e6cfce3b460 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Wed, 5 Nov 2014 22:34:59 +0100 Subject: [PATCH 01/15] Renamed AcmeHelloBundle to AppBundle --- book/controller.rst | 59 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index 45c21c21f5b..ddad3d0d2bf 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -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; @@ -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 @@ -163,7 +162,7 @@ to the controller: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeHelloBundle:Hello:index + AppBundle:Hello:index @@ -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; @@ -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, @@ -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; @@ -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 @@ -255,7 +254,7 @@ example: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeHelloBundle:Hello:index + AppBundle:Hello:index green @@ -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', ))); @@ -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; @@ -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', )); @@ -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:: @@ -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', ); From 0ef1c68a18ecd03d5434e1693b1778d1e718ee8a Mon Sep 17 00:00:00 2001 From: WouterJ Date: Wed, 5 Nov 2014 22:35:27 +0100 Subject: [PATCH 02/15] Moved templates to app/Resources/views --- best_practices/templates.rst | 2 ++ book/controller.rst | 42 +++++++++++++----------------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/best_practices/templates.rst b/best_practices/templates.rst index 4783cb37aa7..36b6267ab30 100644 --- a/best_practices/templates.rst +++ b/best_practices/templates.rst @@ -22,6 +22,8 @@ In addition, Twig is the only template format with guaranteed support in Symfony 3.0. As a matter of fact, PHP may be removed from the officially supported template engines. +.. _best_practices-template-location: + Template Locations ------------------ diff --git a/book/controller.rst b/book/controller.rst index ddad3d0d2bf..b4a255e811c 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -539,33 +539,28 @@ content from the template can be used to create a ``Response`` object:: use Symfony\Component\HttpFoundation\Response; - $content = $this->renderView( - 'AcmeHelloBundle:Hello:index.html.twig', - array('name' => $name) - ); + $content = $this->renderView('Hello/index.html.twig', array('name' => $name)); return new Response($content); This can even be done in just one step with the ``render()`` method, which returns a ``Response`` object containing the content from the template:: - return $this->render( - 'AcmeHelloBundle:Hello:index.html.twig', - array('name' => $name) - ); + return $this->render('Hello/index.html.twig', array('name' => $name)); -In both cases, the ``Resources/views/Hello/index.html.twig`` template inside -the ``AcmeHelloBundle`` will be rendered. +In both cases, the ``app/Resources/views/Hello/index.html.twig`` template will +be rendered. -The Symfony templating engine is explained in great detail in the -:doc:`Templating ` chapter. +.. sidebar:: Referencing Templates that Live inside the Bundle -.. tip:: + You can also put templates in the ``Resources/views`` directory of a + bundle. You can then reference is with the + ``BundleName:DirectoryName:FileName`` syntax. E.g. + ``AppBundle:Hello:index.html.twig`` would refer to the template located in + ``src/AppBundle/Resources/views/Hello/index.html.twig``. - You can even avoid calling the ``render`` method by using the ``@Template`` - annotation. See the - :doc:`FrameworkExtraBundle documentation ` - more details. +The Symfony templating engine is explained in great detail in the +:doc:`Templating ` chapter. .. tip:: @@ -573,10 +568,7 @@ The Symfony templating engine is explained in great detail in the service. The ``templating`` service can also be used directly:: $templating = $this->get('templating'); - $content = $templating->render( - 'AcmeHelloBundle:Hello:index.html.twig', - array('name' => $name) - ); + $content = $templating->render('Hello/index.html.twig', array('name' => $name)); .. note:: @@ -584,12 +576,8 @@ The Symfony templating engine is explained in great detail in the be careful to avoid the pitfall of making your directory structure unduly elaborate:: - $templating->render( - 'AcmeHelloBundle:Hello/Greetings:index.html.twig', - array('name' => $name) - ); - // index.html.twig found in Resources/views/Hello/Greetings - // is rendered. + $templating->render('Hello/Greetings/index.html.twig', array('name' => $name)); + // renders app/Resources/views/Hello/Greetings/index.html.twig .. index:: single: Controller; Accessing services From b7356429657e0c4dbc0c54b5406f54d4e260f7dd Mon Sep 17 00:00:00 2001 From: WouterJ Date: Wed, 5 Nov 2014 22:36:07 +0100 Subject: [PATCH 03/15] Fixed some other minor stuff --- book/controller.rst | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index b4a255e811c..9e2cc2dd3b4 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -201,7 +201,8 @@ see :ref:`controller-string-syntax`. .. tip:: - You can learn much more about the routing system in the :doc:`Routing chapter `. + You can learn much more about the routing system in the + :doc:`Routing chapter `. .. index:: single: Controller; Controller arguments @@ -225,15 +226,15 @@ to that method:: { public function indexAction($name) { - // ... + // ... } } The controller has a single argument, ``$name``, which corresponds to the ``{name}`` parameter from the matched route (``ryan`` in the example). In fact, when executing your controller, Symfony matches each argument of -the controller with a parameter from the matched route. Take the following -example: +the controller with a parameter from the matched route by its name. Take the +following example: .. configuration-block:: @@ -421,7 +422,7 @@ Common Controller Tasks Though a controller can do virtually anything, most controllers will perform the same basic tasks over and over again. These tasks, such as redirecting, forwarding, rendering templates and accessing core services, are very easy -to manage in Symfony. +to manage in Symfony when you're extending the base ``Controller`` class. .. index:: single: Controller; Redirecting @@ -429,7 +430,9 @@ to manage in Symfony. Redirecting ~~~~~~~~~~~ -If you want to redirect the user to another page, use the ``redirect()`` method:: +If you want to redirect the user to another page, use the +:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::redirect` +method:: public function indexAction() { From c8f5ad53ec76ea513572a8c9763411fdb74a0712 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Wed, 5 Nov 2014 23:39:03 +0100 Subject: [PATCH 04/15] Updated to use AppBundle --- book/from_flat_php_to_symfony2.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index dc1e533411e..bf01441ff14 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -547,8 +547,8 @@ from scratch, you could at least use Symfony's standalone `Routing`_ and Instead of re-solving common problems, you can let Symfony take care of them for you. Here's the same sample application, now built in Symfony:: - // src/Acme/BlogBundle/Controller/BlogController.php - namespace Acme\BlogBundle\Controller; + // src/AppBundle/Controller/BlogController.php + namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -571,7 +571,7 @@ them for you. Here's the same sample application, now built in Symfony:: { $post = $this->get('doctrine') ->getManager() - ->getRepository('AcmeBlogBundle:Post') + ->getRepository('AppBundle:Post') ->find($id); if (!$post) { @@ -644,11 +644,11 @@ A routing configuration map provides this information in a readable format: # app/config/routing.yml blog_list: path: /blog - defaults: { _controller: AcmeBlogBundle:Blog:list } + defaults: { _controller: AppBundle:Blog:list } blog_show: path: /blog/show/{id} - defaults: { _controller: AcmeBlogBundle:Blog:show } + defaults: { _controller: AppBundle:Blog:show } Now that Symfony is handling all the mundane tasks, the front controller is dead simple. And since it does so little, you'll never have to touch From 18e3a317472e5418649277fa61645a74778a0fcc Mon Sep 17 00:00:00 2001 From: WouterJ Date: Wed, 5 Nov 2014 23:40:18 +0100 Subject: [PATCH 05/15] Moved templates to app --- book/from_flat_php_to_symfony2.rst | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index bf01441ff14..4bb3252e6dd 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -561,10 +561,7 @@ them for you. Here's the same sample application, now built in Symfony:: ->createQuery('SELECT p FROM AcmeBlogBundle:Post p') ->execute(); - return $this->render( - 'AcmeBlogBundle:Blog:list.html.php', - array('posts' => $posts) - ); + return $this->render('Blog/list.html.php', array('posts' => $posts)); } public function showAction($id) @@ -579,10 +576,7 @@ them for you. Here's the same sample application, now built in Symfony:: throw $this->createNotFoundException(); } - return $this->render( - 'AcmeBlogBundle:Blog:show.html.php', - array('post' => $post) - ); + return $this->render('Blog/show.html.php', array('post' => $post)); } } @@ -593,8 +587,8 @@ now quite a bit simpler: .. code-block:: html+php - - extend('::layout.html.php') ?> + + extend('layout.html.php') ?> set('title', 'List of Posts') ?> @@ -716,8 +710,8 @@ for example, the list template written in Twig: .. code-block:: html+jinja - {# src/Acme/BlogBundle/Resources/views/Blog/list.html.twig #} - {% extends "::layout.html.twig" %} + {# app/Resources/views/Blog/list.html.twig #} + {% extends "layout.html.twig" %} {% block title %}List of Posts{% endblock %} From ab2e2c71576dd2d88c7eb5d4872fe66b19c282ed Mon Sep 17 00:00:00 2001 From: WouterJ Date: Wed, 5 Nov 2014 23:40:28 +0100 Subject: [PATCH 06/15] Other fixes --- book/from_flat_php_to_symfony2.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index 4bb3252e6dd..b59e8fcc948 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -580,10 +580,10 @@ them for you. Here's the same sample application, now built in Symfony:: } } -The two controllers are still lightweight. Each uses the :doc:`Doctrine ORM library ` -to retrieve objects from the database and the Templating component to -render a template and return a ``Response`` object. The list template is -now quite a bit simpler: +The two controllers are still lightweight. Each uses the +:doc:`Doctrine ORM library ` to retrieve objects from the +database and the Templating component to render a template and return a +``Response`` object. The list template is now quite a bit simpler: .. code-block:: html+php From db3086d3351cdca57eb280f6fdfb93835a5f863a Mon Sep 17 00:00:00 2001 From: WouterJ Date: Thu, 6 Nov 2014 13:32:44 +0100 Subject: [PATCH 07/15] Moved templates to app and controllers to AppBundle --- book/templating.rst | 164 ++++++++++++++++++++++---------------------- 1 file changed, 81 insertions(+), 83 deletions(-) diff --git a/book/templating.rst b/book/templating.rst index 8dd65e51267..c6a7c04c57b 100644 --- a/book/templating.rst +++ b/book/templating.rst @@ -260,8 +260,8 @@ A child template might look like this: .. code-block:: html+jinja - {# src/Acme/BlogBundle/Resources/views/Blog/index.html.twig #} - {% extends '::base.html.twig' %} + {# app/Resources/views/Blog/index.html.twig #} + {% extends 'base.html.twig' %} {% block title %}My cool blog posts{% endblock %} @@ -274,8 +274,8 @@ A child template might look like this: .. code-block:: html+php - - extend('::base.html.php') ?> + + extend('base.html.php') ?> set('title', 'My cool blog posts') ?> @@ -289,9 +289,10 @@ A child template might look like this: .. note:: The parent template is identified by a special string syntax - (``::base.html.twig``) that indicates that the template lives in the - ``app/Resources/views`` directory of the project. This naming convention is - explained fully in :ref:`template-naming-locations`. + (``base.html.twig``). This path is relative to the ``app/Resources/views`` + directory of the project. You could also use the logical name equivalent: + ``::base.html.twig``. This naming convention is explained fully in + :ref:`template-naming-locations`. The key to template inheritance is the ``{% extends %}`` tag. This tells the templating engine to first evaluate the base template, which sets up @@ -381,13 +382,14 @@ Template Naming and Locations By default, templates can live in two different locations: * ``app/Resources/views/``: The applications ``views`` directory can contain - application-wide base templates (i.e. your application's layouts) as well as - templates that override bundle templates (see - :ref:`overriding-bundle-templates`); + application-wide base templates (i.e. your application's layouts and + templates of the application bundle) as well as templates that override + third party bundle templates (see :ref:`overriding-bundle-templates`); -* ``path/to/bundle/Resources/views/``: Each bundle houses its templates in its - ``Resources/views`` directory (and subdirectories). The majority of templates - will live inside a bundle. +* ``path/to/bundle/Resources/views/``: Each third party bundle houses its + templates in its ``Resources/views`` directory (and subdirectories). When you + plan to share your bundle, you should put the templates in the shared bundle + instead of the ``app/Resources/views`` directory. Symfony uses a **bundle**:**controller**:**template** string syntax for templates. This allows for several different types of templates, each which @@ -422,6 +424,9 @@ lives in a specific location: that the template is not located in any bundle, but instead in the root ``app/Resources/views/`` directory. +* ``base.html.twig``: Equivalent to ``::base.html.twig`` and **recommended** + for application-wide templates. + In the :ref:`overriding-bundle-templates` section, you'll find out how each template living inside the ``AcmeBlogBundle``, for example, can be overridden by placing a template of the same name in the ``app/Resources/AcmeBlogBundle/views/`` @@ -435,15 +440,12 @@ directory. This gives the power to override templates from any vendor bundle. Template Suffix ~~~~~~~~~~~~~~~ -The **bundle**:**controller**:**template** format of each template specifies -*where* the template file is located. Every template name also has two extensions -that specify the *format* and *engine* for that template. - -* **AcmeBlogBundle:Blog:index.html.twig** - HTML format, Twig engine +Every template name also has two extensions that specify the *format* and +*engine* for that template. -* **AcmeBlogBundle:Blog:index.html.php** - HTML format, PHP engine - -* **AcmeBlogBundle:Blog:index.css.twig** - CSS format, Twig engine +* **Blog/index.html.twig** - HTML format, Twig engine +* **Blog/index.html.php** - HTML format, PHP engine +* **Blog/index.css.twig** - CSS format, Twig engine By default, any Symfony template can be written in either Twig or PHP, and the last part of the extension (e.g. ``.twig`` or ``.php``) specifies which @@ -505,7 +507,7 @@ template. First, create the template that you'll need to reuse. .. code-block:: html+jinja - {# src/Acme/ArticleBundle/Resources/views/Article/articleDetails.html.twig #} + {# app/Resources/views/Article/articleDetails.html.twig #}

{{ article.title }}

@@ -515,7 +517,7 @@ template. First, create the template that you'll need to reuse. .. code-block:: html+php - +

getTitle() ?>

@@ -529,31 +531,28 @@ Including this template from any other template is simple: .. code-block:: html+jinja - {# src/Acme/ArticleBundle/Resources/views/Article/list.html.twig #} - {% extends 'AcmeArticleBundle::layout.html.twig' %} + {# app/Resources/views/Article/list.html.twig #} + {% extends 'layout.html.twig' %} {% block body %}

Recent Articles

{% for article in articles %} - {{ include( - 'AcmeArticleBundle:Article:articleDetails.html.twig', - { 'article': article } - ) }} + {{ include('Article/articleDetails.html.twig', { 'article': article }) }} {% endfor %} {% endblock %} .. code-block:: html+php - - extend('AcmeArticleBundle::layout.html.php') ?> + + extend('layout.html.php') ?> start('body') ?>

Recent Articles

render( - 'AcmeArticleBundle:Article:articleDetails.html.php', + 'Article/articleDetails.html.php', array('article' => $article) ) ?> @@ -593,7 +592,7 @@ The solution is to simply embed the result of an entire controller from your template. First, create a controller that renders a certain number of recent articles:: - // src/Acme/ArticleBundle/Controller/ArticleController.php + // src/AppBundle/Controller/ArticleController.php class ArticleController extends Controller { public function recentArticlesAction($max = 3) @@ -603,7 +602,7 @@ articles:: $articles = ...; return $this->render( - 'AcmeArticleBundle:Article:recentList.html.twig', + 'Article/recentList.html.twig', array('articles' => $articles) ); } @@ -615,7 +614,7 @@ The ``recentList`` template is perfectly straightforward: .. code-block:: html+jinja - {# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #} + {# app/Resources/views/Article/recentList.html.twig #} {% for article in articles %} {{ article.title }} @@ -624,7 +623,7 @@ The ``recentList`` template is perfectly straightforward: .. code-block:: html+php - + getTitle() ?> @@ -648,9 +647,10 @@ string syntax for controllers (i.e. **bundle**:**controller**:**action**): {# ... #} .. code-block:: html+php @@ -756,7 +756,7 @@ in your application configuration: framework: # ... templating: - hinclude_default_template: AcmeDemoBundle::hinclude.html.twig + hinclude_default_template: hinclude.html.twig .. code-block:: xml @@ -770,7 +770,7 @@ in your application configuration: - + @@ -781,7 +781,7 @@ in your application configuration: // ... 'templating' => array( 'hinclude_default_template' => array( - 'AcmeDemoBundle::hinclude.html.twig', + 'hinclude.html.twig', ), ), )); @@ -797,7 +797,7 @@ any global default template that is defined): .. code-block:: jinja {{ render_hinclude(controller('...'), { - 'default': 'AcmeDemoBundle:Default:content.html.twig' + 'default': 'Default/content.html.twig' }) }} .. code-block:: php @@ -806,7 +806,7 @@ any global default template that is defined): new ControllerReference('...'), array( 'renderer' => 'hinclude', - 'default' => 'AcmeDemoBundle:Default:content.html.twig', + 'default' => 'Default/content.html.twig', ) ) ?> @@ -853,7 +853,7 @@ configuration: # app/config/routing.yml _welcome: path: / - defaults: { _controller: AcmeDemoBundle:Welcome:index } + defaults: { _controller: AppBundle:Welcome:index } .. code-block:: xml @@ -865,7 +865,7 @@ configuration: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeDemoBundle:Welcome:index + AppBundle:Welcome:index @@ -877,7 +877,7 @@ configuration: $collection = new RouteCollection(); $collection->add('_welcome', new Route('/', array( - '_controller' => 'AcmeDemoBundle:Welcome:index', + '_controller' => 'AppBundle:Welcome:index', ))); return $collection; @@ -904,7 +904,7 @@ route: # app/config/routing.yml article_show: path: /article/{slug} - defaults: { _controller: AcmeArticleBundle:Article:show } + defaults: { _controller: AppBundle:Article:show } .. code-block:: xml @@ -916,7 +916,7 @@ route: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeArticleBundle:Article:show + AppBundle:Article:show @@ -928,7 +928,7 @@ route: $collection = new RouteCollection(); $collection->add('article_show', new Route('/article/{slug}', array( - '_controller' => 'AcmeArticleBundle:Article:show', + '_controller' => 'AppBundle:Article:show', ))); return $collection; @@ -942,7 +942,7 @@ correctly: .. code-block:: html+jinja - {# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #} + {# app/Resources/views/Article/recentList.html.twig #} {% for article in articles %} {{ article.title }} @@ -951,7 +951,7 @@ correctly: .. code-block:: html+php - + Blog Application @@ -1329,12 +1329,12 @@ covered: * Create individual templates for each page and make each extend the appropriate section template. For example, the "index" page would be called something - close to ``AcmeBlogBundle:Blog:index.html.twig`` and list the actual blog posts. + close to ``Blog/index.html.twig`` and list the actual blog posts. .. code-block:: html+jinja - {# src/Acme/BlogBundle/Resources/views/Blog/index.html.twig #} - {% extends 'AcmeBlogBundle::layout.html.twig' %} + {# app/Resources/views/Blog/index.html.twig #} + {% extends 'Blog/layout.html.twig' %} {% block content %} {% for entry in blog_entries %} @@ -1343,15 +1343,15 @@ covered: {% endfor %} {% endblock %} -Notice that this template extends the section template (``AcmeBlogBundle::layout.html.twig``) -which in-turn extends the base application layout (``::base.html.twig``). -This is the common three-level inheritance model. +Notice that this template extends the section template (``Blog/layout.html.twig``) +which in-turn extends the base application layout (``base.html.twig``). This is +the common three-level inheritance model. When building your application, you may choose to follow this method or simply make each page template extend the base application template directly -(e.g. ``{% extends '::base.html.twig' %}``). The three-template model is -a best-practice method used by vendor bundles so that the base template for -a bundle can be easily overridden to properly extend your application's base +(e.g. ``{% extends 'base.html.twig' %}``). The three-template model is a +best-practice method used by vendor bundles so that the base template for a +bundle can be easily overridden to properly extend your application's base layout. .. index:: @@ -1455,15 +1455,16 @@ in a JavaScript string, use the ``js`` context: Debugging --------- -When using PHP, you can use ``var_dump()`` if you need to quickly find the -value of a variable passed. This is useful, for example, inside your controller. -The same can be achieved when using Twig thanks to the debug extension. +When using PHP, you can use :phpfunction:`var_dump` if you need to quickly find +the value of a variable passed. This is useful, for example, inside your +controller. The same can be achieved when using Twig thanks to the debug +extension. Template parameters can then be dumped using the ``dump`` function: .. code-block:: html+jinja - {# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #} + {# app/Resources/views/Article/recentList.html.twig #} {{ dump(articles) }} {% for article in articles %} @@ -1485,13 +1486,10 @@ console command: .. code-block:: bash # You can check by filename: - $ php app/console twig:lint src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig + $ php app/console twig:lint app/Resources/views/Article/recentList.html.twig # or by directory: - $ php app/console twig:lint src/Acme/ArticleBundle/Resources/views - - # or using the bundle name: - $ php app/console twig:lint @AcmeArticleBundle + $ php app/console twig:lint app/Resources/views .. _template-formats: @@ -1506,7 +1504,7 @@ For example, the same "resource" is often rendered in several different formats. To render an article index page in XML, simply include the format in the template name: -* *XML template name*: ``AcmeArticleBundle:Article:index.xml.twig`` +* *XML template name*: ``Article/index.xml.twig`` * *XML template filename*: ``index.xml.twig`` In reality, this is nothing more than a naming convention and the template @@ -1520,7 +1518,7 @@ pattern is to do the following:: { $format = $request->getRequestFormat(); - return $this->render('AcmeBlogBundle:Blog:index.'.$format.'.twig'); + return $this->render('Blog/index.'.$format.'.twig'); } The ``getRequestFormat`` on the ``Request`` object defaults to ``html``, @@ -1560,7 +1558,7 @@ their use is not mandatory. The ``Response`` object returned by a controller can be created with or without the use of a template:: // creates a Response object whose content is the rendered template - $response = $this->render('AcmeArticleBundle:Article:index.html.twig'); + $response = $this->render('Article/index.html.twig'); // creates a Response object whose content is simple text $response = new Response('response content'); From a8a75d79de3834744ad12061ae239028e0e0bfec Mon Sep 17 00:00:00 2001 From: WouterJ Date: Thu, 6 Nov 2014 13:34:39 +0100 Subject: [PATCH 08/15] Changed Bundle:Controller:Template to bundle:dir:file --- book/templating.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/book/templating.rst b/book/templating.rst index c6a7c04c57b..7d90790bf67 100644 --- a/book/templating.rst +++ b/book/templating.rst @@ -391,9 +391,10 @@ By default, templates can live in two different locations: plan to share your bundle, you should put the templates in the shared bundle instead of the ``app/Resources/views`` directory. -Symfony uses a **bundle**:**controller**:**template** string syntax for -templates. This allows for several different types of templates, each which -lives in a specific location: +Symfony uses a **bundle**:**directory**:**filename** string syntax for +templates. You can also give paths, which are relative to the +``app/Resources/views`` directory. This allows for several different types of +templates, each which lives in a specific location: * ``AcmeBlogBundle:Blog:index.html.twig``: This syntax is used to specify a template for a specific page. The three parts of the string, each separated @@ -402,26 +403,26 @@ lives in a specific location: * ``AcmeBlogBundle``: (*bundle*) the template lives inside the ``AcmeBlogBundle`` (e.g. ``src/Acme/BlogBundle``); - * ``Blog``: (*controller*) indicates that the template lives inside the + * ``Blog``: (*directory*) indicates that the template lives inside the ``Blog`` subdirectory of ``Resources/views``; - * ``index.html.twig``: (*template*) the actual name of the file is + * ``index.html.twig``: (*filename*) the actual name of the file is ``index.html.twig``. Assuming that the ``AcmeBlogBundle`` lives at ``src/Acme/BlogBundle``, the final path to the layout would be ``src/Acme/BlogBundle/Resources/views/Blog/index.html.twig``. * ``AcmeBlogBundle::layout.html.twig``: This syntax refers to a base template - that's specific to the ``AcmeBlogBundle``. Since the middle, "controller", + that's specific to the ``AcmeBlogBundle``. Since the middle, "directory", portion is missing (e.g. ``Blog``), the template lives at ``Resources/views/layout.html.twig`` inside ``AcmeBlogBundle``. Yes, there are 2 colons in the middle of the string when the "controller" subdirectory part is missing. -* ``::base.html.twig``: This syntax refers to an application-wide base template - or layout. Notice that the string begins with two colons (``::``), meaning - that both the *bundle* and *controller* portions are missing. This means - that the template is not located in any bundle, but instead in the root +* ``::base.html.twig``: This syntax refers to views in ``app/Resources/views``. + Notice that the string begins with two colons (``::``), meaning that both the + *bundle* and *directory* portions are missing. This means that the template + is not located in any bundle, but instead in the root ``app/Resources/views/`` directory. * ``base.html.twig``: Equivalent to ``::base.html.twig`` and **recommended** From 10b3c7c8d6b3da1e97ddee060bc9b5f346456d9e Mon Sep 17 00:00:00 2001 From: WouterJ Date: Thu, 6 Nov 2014 13:34:47 +0100 Subject: [PATCH 09/15] Other random fixes --- book/templating.rst | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/book/templating.rst b/book/templating.rst index 7d90790bf67..413c769b1bb 100644 --- a/book/templating.rst +++ b/book/templating.rst @@ -16,8 +16,8 @@ code. .. note:: - How to render templates is covered in the :ref:`controller ` - page of the book. + How to render templates is covered in the + :ref:`controller ` page of the book. .. index:: single: Templating; What is a template? @@ -355,15 +355,15 @@ When working with template inheritance, here are some tips to keep in mind: can use the ``{{ parent() }}`` function. This is useful if you want to add to the contents of a parent block instead of completely overriding it: - .. code-block:: html+jinja + .. code-block:: html+jinja - {% block sidebar %} -

Table of Contents

+ {% block sidebar %} +

Table of Contents

- {# ... #} + {# ... #} - {{ parent() }} - {% endblock %} + {{ parent() }} + {% endblock %} .. index:: single: Templating; Naming conventions @@ -444,9 +444,13 @@ Template Suffix Every template name also has two extensions that specify the *format* and *engine* for that template. -* **Blog/index.html.twig** - HTML format, Twig engine -* **Blog/index.html.php** - HTML format, PHP engine -* **Blog/index.css.twig** - CSS format, Twig engine +======================== ====== ====== +Filename Format Engine +======================== ====== ====== +``Blog/index.html.twig`` HTML Twig +``Blog/index.html.php`` HTML PHP +``Blog/index.css.twig`` CSS Twig +======================== ====== ====== By default, any Symfony template can be written in either Twig or PHP, and the last part of the extension (e.g. ``.twig`` or ``.php``) specifies which @@ -556,7 +560,7 @@ Including this template from any other template is simple: 'Article/articleDetails.html.php', array('article' => $article) ) ?> - + stop() ?> The template is included using the ``{{ include() }}`` function. Notice that the @@ -594,6 +598,10 @@ template. First, create a controller that renders a certain number of recent articles:: // src/AppBundle/Controller/ArticleController.php + namespace AppBundle\Controller; + + // ... + class ArticleController extends Controller { public function recentArticlesAction($max = 3) @@ -1227,11 +1235,11 @@ bundles (see `KnpBundles.com`_) for a large number of different features. Once you use a third-party bundle, you'll likely need to override and customize one or more of its templates. -Suppose you've included the imaginary open-source ``AcmeBlogBundle`` in your -project (e.g. in the ``src/Acme/BlogBundle`` directory). And while you're -really happy with everything, you want to override the blog "list" page to -customize the markup specifically for your application. By digging into the -``Blog`` controller of the ``AcmeBlogBundle``, you find the following:: +Suppose you've installed the imaginary open-source ``AcmeBlogBundle`` in your +project. And while you're really happy with everything, you want to override +the blog "list" page to customize the markup specifically for your application. +By digging into the ``Blog`` controller of the ``AcmeBlogBundle``, you find the +following:: public function indexAction() { From 4da1bcf22e1395575eb673c1cc6ee314065d42dc Mon Sep 17 00:00:00 2001 From: WouterJ Date: Thu, 6 Nov 2014 13:49:16 +0100 Subject: [PATCH 10/15] Use AppBundle --- book/routing.rst | 146 +++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index 6dbaf109cae..7a61a15ccd6 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -39,7 +39,7 @@ The route is simple: # app/config/routing.yml blog_show: path: /blog/{slug} - defaults: { _controller: AcmeBlogBundle:Blog:show } + defaults: { _controller: AppBundle:Blog:show } .. code-block:: xml @@ -51,7 +51,7 @@ The route is simple: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeBlogBundle:Blog:show + AppBundle:Blog:show @@ -63,7 +63,7 @@ The route is simple: $collection = new RouteCollection(); $collection->add('blog_show', new Route('/blog/{slug}', array( - '_controller' => 'AcmeBlogBundle:Blog:show', + '_controller' => 'AppBundle:Blog:show', ))); return $collection; @@ -84,8 +84,8 @@ should be executed when a URL matches this route. The ``_controller`` string is called the :ref:`logical name `. It follows a pattern that points to a specific PHP class and method:: - // src/Acme/BlogBundle/Controller/BlogController.php - namespace Acme\BlogBundle\Controller; + // src/AppBundle/Controller/BlogController.php + namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -94,9 +94,9 @@ pattern that points to a specific PHP class and method:: public function showAction($slug) { // use the $slug variable to query the database - $blog = ...; + $article = ...; - return $this->render('AcmeBlogBundle:Blog:show.html.twig', array( + return $this->render('Blog/show.html.twig', array( 'blog' => $blog, )); } @@ -213,7 +213,7 @@ A basic route consists of just two parts: the ``path`` to match and a # app/config/routing.yml _welcome: path: / - defaults: { _controller: AcmeDemoBundle:Main:homepage } + defaults: { _controller: AppBundle:Main:homepage } .. code-block:: xml @@ -225,7 +225,7 @@ A basic route consists of just two parts: the ``path`` to match and a http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeDemoBundle:Main:homepage + AppBundle:Main:homepage @@ -238,12 +238,12 @@ A basic route consists of just two parts: the ``path`` to match and a $collection = new RouteCollection(); $collection->add('_welcome', new Route('/', array( - '_controller' => 'AcmeDemoBundle:Main:homepage', + '_controller' => 'AppBundle:Main:homepage', ))); return $collection; -This route matches the homepage (``/``) and maps it to the ``AcmeDemoBundle:Main:homepage`` +This route matches the homepage (``/``) and maps it to the ``AppBundle:Main:homepage`` controller. The ``_controller`` string is translated by Symfony into an actual PHP function and executed. That process will be explained shortly in the :ref:`controller-string-syntax` section. @@ -264,7 +264,7 @@ routes will contain one or more named "wildcard" placeholders: # app/config/routing.yml blog_show: path: /blog/{slug} - defaults: { _controller: AcmeBlogBundle:Blog:show } + defaults: { _controller: AppBundle:Blog:show } .. code-block:: xml @@ -276,7 +276,7 @@ routes will contain one or more named "wildcard" placeholders: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeBlogBundle:Blog:show + AppBundle:Blog:show @@ -288,7 +288,7 @@ routes will contain one or more named "wildcard" placeholders: $collection = new RouteCollection(); $collection->add('blog_show', new Route('/blog/{slug}', array( - '_controller' => 'AcmeBlogBundle:Blog:show', + '_controller' => 'AppBundle:Blog:show', ))); return $collection; @@ -316,7 +316,7 @@ the available blog posts for this imaginary blog application: # app/config/routing.yml blog: path: /blog - defaults: { _controller: AcmeBlogBundle:Blog:index } + defaults: { _controller: AppBundle:Blog:index } .. code-block:: xml @@ -328,7 +328,7 @@ the available blog posts for this imaginary blog application: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeBlogBundle:Blog:index + AppBundle:Blog:index @@ -340,7 +340,7 @@ the available blog posts for this imaginary blog application: $collection = new RouteCollection(); $collection->add('blog', new Route('/blog', array( - '_controller' => 'AcmeBlogBundle:Blog:index', + '_controller' => 'AppBundle:Blog:index', ))); return $collection; @@ -357,7 +357,7 @@ entries? Update the route to have a new ``{page}`` placeholder: # app/config/routing.yml blog: path: /blog/{page} - defaults: { _controller: AcmeBlogBundle:Blog:index } + defaults: { _controller: AppBundle:Blog:index } .. code-block:: xml @@ -369,7 +369,7 @@ entries? Update the route to have a new ``{page}`` placeholder: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeBlogBundle:Blog:index + AppBundle:Blog:index @@ -381,7 +381,7 @@ entries? Update the route to have a new ``{page}`` placeholder: $collection = new RouteCollection(); $collection->add('blog', new Route('/blog/{page}', array( - '_controller' => 'AcmeBlogBundle:Blog:index', + '_controller' => 'AppBundle:Blog:index', ))); return $collection; @@ -403,7 +403,7 @@ This is done by including it in the ``defaults`` collection: # app/config/routing.yml blog: path: /blog/{page} - defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 } + defaults: { _controller: AppBundle:Blog:index, page: 1 } .. code-block:: xml @@ -415,7 +415,7 @@ This is done by including it in the ``defaults`` collection: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeBlogBundle:Blog:index + AppBundle:Blog:index 1 @@ -428,7 +428,7 @@ This is done by including it in the ``defaults`` collection: $collection = new RouteCollection(); $collection->add('blog', new Route('/blog/{page}', array( - '_controller' => 'AcmeBlogBundle:Blog:index', + '_controller' => 'AppBundle:Blog:index', 'page' => 1, ))); @@ -476,11 +476,11 @@ Take a quick look at the routes that have been created so far: # app/config/routing.yml blog: path: /blog/{page} - defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 } + defaults: { _controller: AppBundle:Blog:index, page: 1 } blog_show: path: /blog/{slug} - defaults: { _controller: AcmeBlogBundle:Blog:show } + defaults: { _controller: AppBundle:Blog:show } .. code-block:: xml @@ -492,12 +492,12 @@ Take a quick look at the routes that have been created so far: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeBlogBundle:Blog:index + AppBundle:Blog:index 1 - AcmeBlogBundle:Blog:show + AppBundle:Blog:show @@ -509,12 +509,12 @@ Take a quick look at the routes that have been created so far: $collection = new RouteCollection(); $collection->add('blog', new Route('/blog/{page}', array( - '_controller' => 'AcmeBlogBundle:Blog:index', + '_controller' => 'AppBundle:Blog:index', 'page' => 1, ))); $collection->add('blog_show', new Route('/blog/{show}', array( - '_controller' => 'AcmeBlogBundle:Blog:show', + '_controller' => 'AppBundle:Blog:show', ))); return $collection; @@ -546,7 +546,7 @@ requirements can easily be added for each parameter. For example: # app/config/routing.yml blog: path: /blog/{page} - defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 } + defaults: { _controller: AppBundle:Blog:index, page: 1 } requirements: page: \d+ @@ -560,7 +560,7 @@ requirements can easily be added for each parameter. For example: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeBlogBundle:Blog:index + AppBundle:Blog:index 1 \d+ @@ -574,7 +574,7 @@ requirements can easily be added for each parameter. For example: $collection = new RouteCollection(); $collection->add('blog', new Route('/blog/{page}', array( - '_controller' => 'AcmeBlogBundle:Blog:index', + '_controller' => 'AppBundle:Blog:index', 'page' => 1, ), array( 'page' => '\d+', @@ -621,7 +621,7 @@ URL: # app/config/routing.yml homepage: path: /{culture} - defaults: { _controller: AcmeDemoBundle:Main:homepage, culture: en } + defaults: { _controller: AppBundle:Main:homepage, culture: en } requirements: culture: en|fr @@ -635,7 +635,7 @@ URL: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeDemoBundle:Main:homepage + AppBundle:Main:homepage en en|fr @@ -649,7 +649,7 @@ URL: $collection = new RouteCollection(); $collection->add('homepage', new Route('/{culture}', array( - '_controller' => 'AcmeDemoBundle:Main:homepage', + '_controller' => 'AppBundle:Main:homepage', 'culture' => 'en', ), array( 'culture' => 'en|fr', @@ -689,12 +689,12 @@ be accomplished with the following route configuration: # app/config/routing.yml contact: path: /contact - defaults: { _controller: AcmeDemoBundle:Main:contact } + defaults: { _controller: AppBundle:Main:contact } methods: [GET] contact_process: path: /contact - defaults: { _controller: AcmeDemoBundle:Main:contactProcess } + defaults: { _controller: AppBundle:Main:contactProcess } methods: [POST] .. code-block:: xml @@ -707,11 +707,11 @@ be accomplished with the following route configuration: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeDemoBundle:Main:contact + AppBundle:Main:contact - AcmeDemoBundle:Main:contactProcess + AppBundle:Main:contactProcess @@ -723,11 +723,11 @@ be accomplished with the following route configuration: $collection = new RouteCollection(); $collection->add('contact', new Route('/contact', array( - '_controller' => 'AcmeDemoBundle:Main:contact', + '_controller' => 'AppBundle:Main:contact', ), array(), array(), '', array(), array('GET'))); $collection->add('contact_process', new Route('/contact', array( - '_controller' => 'AcmeDemoBundle:Main:contactProcess', + '_controller' => 'AppBundle:Main:contactProcess', ), array(), array(), '', array(), array('POST'))); return $collection; @@ -775,7 +775,7 @@ routing system can be: # app/config/routing.yml article_show: path: /articles/{culture}/{year}/{title}.{_format} - defaults: { _controller: AcmeDemoBundle:Article:show, _format: html } + defaults: { _controller: AppBundle:Article:show, _format: html } requirements: culture: en|fr _format: html|rss @@ -793,7 +793,7 @@ routing system can be: - AcmeDemoBundle:Article:show + AppBundle:Article:show html en|fr html|rss @@ -812,7 +812,7 @@ routing system can be: $collection->add( 'article_show', new Route('/articles/{culture}/{year}/{title}.{_format}', array( - '_controller' => 'AcmeDemoBundle:Article:show', + '_controller' => 'AppBundle:Article:show', '_format' => 'html', ), array( 'culture' => 'en|fr', @@ -882,18 +882,18 @@ each separated by a colon: **bundle**:**controller**:**action** -For example, a ``_controller`` value of ``AcmeBlogBundle:Blog:show`` means: +For example, a ``_controller`` value of ``AppBundle:Blog:show`` means: +----------------+------------------+-------------+ | Bundle | Controller Class | Method Name | +================+==================+=============+ -| AcmeBlogBundle | BlogController | showAction | +| AppBundle | BlogController | showAction | +----------------+------------------+-------------+ The controller might look like this:: - // src/Acme/BlogBundle/Controller/BlogController.php - namespace Acme\BlogBundle\Controller; + // src/AppBundle/Controller/BlogController.php + namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -967,18 +967,18 @@ see :doc:`/cookbook/routing/extra_information`. Including External Routing Resources ------------------------------------ -All routes are loaded via a single configuration file - usually ``app/config/routing.yml`` -(see `Creating Routes`_ above). Commonly, however, you'll want to load routes -from other places, like a routing file that lives inside a bundle. This can -be done by "importing" that file: +All routes are loaded via a single configuration file - usually +``app/config/routing.yml`` (see `Creating Routes`_ above). Commonly, however, +you'll want to load routes from other places, like a routing file that lives +inside a bundle. This can be done by "importing" that file: .. configuration-block:: .. code-block:: yaml # app/config/routing.yml - acme_hello: - resource: "@AcmeHelloBundle/Resources/config/routing.yml" + app: + resource: "@AppBundle/Resources/config/routing.yml" .. code-block:: xml @@ -989,7 +989,7 @@ be done by "importing" that file: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + .. code-block:: php @@ -999,18 +999,18 @@ be done by "importing" that file: $collection = new RouteCollection(); $collection->addCollection( - $loader->import("@AcmeHelloBundle/Resources/config/routing.php") + $loader->import("@AppBundle/Resources/config/routing.php") ); return $collection; .. note:: - When importing resources from YAML, the key (e.g. ``acme_hello``) is meaningless. + When importing resources from YAML, the key (e.g. ``app``) is meaningless. Just be sure that it's unique so no other lines override it. The ``resource`` key loads the given routing resource. In this example the -resource is the full path to a file, where the ``@AcmeHelloBundle`` shortcut +resource is the full path to a file, where the ``@AppBundle`` shortcut syntax resolves to the path of that bundle. The imported file might look like this: @@ -1018,14 +1018,14 @@ like this: .. code-block:: yaml - # src/Acme/HelloBundle/Resources/config/routing.yml - acme_hello: + # src/AppBundle/Resources/config/routing.yml + app: path: /hello/{name} - defaults: { _controller: AcmeHelloBundle:Hello:index } + defaults: { _controller: AppBundle:Hello:index } .. code-block:: xml - + - AcmeHelloBundle:Hello:index + AppBundle:Hello:index .. code-block:: php - // src/Acme/HelloBundle/Resources/config/routing.php + // src/AppBundle/Resources/config/routing.php use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->add('acme_hello', new Route('/hello/{name}', array( - '_controller' => 'AcmeHelloBundle:Hello:index', + '_controller' => 'AppBundle:Hello:index', ))); return $collection; @@ -1057,7 +1057,7 @@ Prefixing Imported Routes ~~~~~~~~~~~~~~~~~~~~~~~~~ You can also choose to provide a "prefix" for the imported routes. For example, -suppose you want the ``acme_hello`` route to have a final path of ``/admin/hello/{name}`` +suppose you want the ``app`` routes to have a final path of ``/admin/hello/{name}`` instead of simply ``/hello/{name}``: .. configuration-block:: @@ -1065,8 +1065,8 @@ instead of simply ``/hello/{name}``: .. code-block:: yaml # app/config/routing.yml - acme_hello: - resource: "@AcmeHelloBundle/Resources/config/routing.yml" + app: + resource: "@AppBundle/Resources/config/routing.yml" prefix: /admin .. code-block:: xml @@ -1079,7 +1079,7 @@ instead of simply ``/hello/{name}``: http://symfony.com/schema/routing/routing-1.0.xsd"> @@ -1088,11 +1088,11 @@ instead of simply ``/hello/{name}``: // app/config/routing.php use Symfony\Component\Routing\RouteCollection; - $acmeHello = $loader->import('@AcmeHelloBundle/Resources/config/routing.php'); - $acmeHello->addPrefix('/admin'); + $app = $loader->import('@AppBundle/Resources/config/routing.php'); + $app->addPrefix('/admin'); $collection = new RouteCollection(); - $collection->addCollection($acmeHello); + $collection->addCollection($app); return $collection; @@ -1177,7 +1177,7 @@ system. Take the ``blog_show`` example route from earlier:: $params = $this->get('router')->match('/blog/my-blog-post'); // array( // 'slug' => 'my-blog-post', - // '_controller' => 'AcmeBlogBundle:Blog:show', + // '_controller' => 'AppBundle:Blog:show', // ) $uri = $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post')); From 210bb4b365d88b2d1fc020ec2c46876f030e7e7d Mon Sep 17 00:00:00 2001 From: WouterJ Date: Thu, 6 Nov 2014 13:50:37 +0100 Subject: [PATCH 11/15] Some minor things --- book/routing.rst | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index 7a61a15ccd6..275433b2044 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -97,7 +97,7 @@ pattern that points to a specific PHP class and method:: $article = ...; return $this->render('Blog/show.html.twig', array( - 'blog' => $blog, + 'article' => $article, )); } } @@ -439,15 +439,13 @@ longer required. The URL ``/blog`` will match this route and the value of the ``page`` parameter will be set to ``1``. The URL ``/blog/2`` will also match, giving the ``page`` parameter a value of ``2``. Perfect. -+--------------------+-------+-----------------------+ -| URL | route | parameters | -+====================+=======+=======================+ -| /blog | blog | {page} = 1 | -+--------------------+-------+-----------------------+ -| /blog/1 | blog | {page} = 1 | -+--------------------+-------+-----------------------+ -| /blog/2 | blog | {page} = 2 | -+--------------------+-------+-----------------------+ +=========== ===== ========== +URL route parameters +=========== ===== ========== +``/blog`` blog {page} = 1 +``/blog/1`` blog {page} = 1 +``/blog/2`` blog {page} = 2 +=========== ===== ========== .. caution:: @@ -660,15 +658,14 @@ URL: For incoming requests, the ``{culture}`` portion of the URL is matched against the regular expression ``(en|fr)``. -+-----+--------------------------+ -| / | {culture} = en | -+-----+--------------------------+ -| /en | {culture} = en | -+-----+--------------------------+ -| /fr | {culture} = fr | -+-----+--------------------------+ -| /es | *won't match this route* | -+-----+--------------------------+ +======= ======================== +path parameters +======= ======================== +``/`` {culture} = en +``/en`` {culture} = en +``/fr`` {culture} = fr +``/es`` *won't match this route* +======= ======================== .. index:: single: Routing; Method requirement @@ -884,11 +881,11 @@ each separated by a colon: For example, a ``_controller`` value of ``AppBundle:Blog:show`` means: -+----------------+------------------+-------------+ -| Bundle | Controller Class | Method Name | -+================+==================+=============+ -| AppBundle | BlogController | showAction | -+----------------+------------------+-------------+ +========= ================== ============== +Bundle Controller Class Method Name +========= ================== ============== +AppBundle ``BlogController`` ``showAction`` +========= ================== ============== The controller might look like this:: From 91c898150ef96bd91a731b88648e739f72411fb2 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 7 Nov 2014 10:47:15 +0100 Subject: [PATCH 12/15] Used annotations for routing --- book/routing.rst | 301 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 243 insertions(+), 58 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index 275433b2044..70036b605b6 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -34,6 +34,25 @@ The route is simple: .. configuration-block:: + .. code-block:: php-annotation + + // src/AppBundle/Controller/BlogController.php + namespace AppBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + + class BlogController extends Controller + { + /** + * @Route("/blog/{slug}") + */ + public function showAction($slug) + { + // ... + } + } + .. code-block:: yaml # app/config/routing.yml @@ -79,28 +98,14 @@ for you to use in your controller (keep reading). The ``blog_show`` is the internal name of the route, which doesn't have any meaning yet and just needs to be unique. Later, you'll use it to generate URLs. -The ``_controller`` parameter is a special key that tells Symfony which controller -should be executed when a URL matches this route. The ``_controller`` string -is called the :ref:`logical name `. It follows a -pattern that points to a specific PHP class and method:: - - // src/AppBundle/Controller/BlogController.php - namespace AppBundle\Controller; - - use Symfony\Bundle\FrameworkBundle\Controller\Controller; - - class BlogController extends Controller - { - public function showAction($slug) - { - // use the $slug variable to query the database - $article = ...; - - return $this->render('Blog/show.html.twig', array( - 'article' => $article, - )); - } - } +If you don't want to use annotations, because you don't like them or because +you don't want to depend on the SensioFrameworkExtraBundle, you can also use +Yaml, XML or PHP. In these formats, the ``_controller`` parameter is a special +key that tells Symfony which controller should be executed when a URL matches +this route. The ``_controller`` string is called the +:ref:`logical name `. It follows a pattern that +points to a specific PHP class and method, in this case the +``AppBundle\Controller\BlogController::showAction`` method. Congratulations! You've just created your first route and connected it to a controller. Now, when you visit ``/blog/my-post``, the ``showAction`` controller @@ -163,7 +168,7 @@ file: # app/config/config.yml framework: # ... - router: { resource: "%kernel.root_dir%/config/routing.yml" } + router: { resource: "%kernel.root_dir%/config/routing.yml" } .. code-block:: xml @@ -208,6 +213,22 @@ A basic route consists of just two parts: the ``path`` to match and a .. configuration-block:: + .. code-block:: php-annotation + + // src/AppBundle/Controller/MainController.php + + // ... + class MainController extends Controller + { + /** + * @Route("/") + */ + public function homepageAction() + { + // ... + } + } + .. code-block:: yaml # app/config/routing.yml @@ -243,10 +264,10 @@ A basic route consists of just two parts: the ``path`` to match and a return $collection; -This route matches the homepage (``/``) and maps it to the ``AppBundle:Main:homepage`` -controller. The ``_controller`` string is translated by Symfony into an -actual PHP function and executed. That process will be explained shortly -in the :ref:`controller-string-syntax` section. +This route matches the homepage (``/``) and maps it to the +``AppBundle:Main:homepage`` controller. The ``_controller`` string is +translated by Symfony into an actual PHP function and executed. That process +will be explained shortly in the :ref:`controller-string-syntax` section. .. index:: single: Routing; Placeholders @@ -259,6 +280,22 @@ routes will contain one or more named "wildcard" placeholders: .. configuration-block:: + .. code-block:: php-annotation + + // src/AppBundle/Controller/BlogController.php + + // ... + class BlogController extends Controller + { + /** + * @Route("/blog/{slug}") + */ + public function showAction($slug) + { + // ... + } + } + .. code-block:: yaml # app/config/routing.yml @@ -311,6 +348,24 @@ the available blog posts for this imaginary blog application: .. configuration-block:: + .. code-block:: php-annotation + + // src/AppBundle/Controller/BlogController.php + + // ... + class BlogController extends Controller + { + // ... + + /** + * @Route("/blog") + */ + public function indexAction() + { + // ... + } + } + .. code-block:: yaml # app/config/routing.yml @@ -352,6 +407,20 @@ entries? Update the route to have a new ``{page}`` placeholder: .. configuration-block:: + .. code-block:: php-annotations + + // src/AppBundle/Controller/BlogController.php + + // ... + + /** + * @Route("/blog/{page}") + */ + public function indexAction($page) + { + // ... + } + .. code-block:: yaml # app/config/routing.yml @@ -398,6 +467,20 @@ This is done by including it in the ``defaults`` collection: .. configuration-block:: + .. code-block:: php-annotations + + // src/AppBundle/Controller/BlogController.php + + // ... + + /** + * @Route("/blog/{page}", defaults={"page" = 1}) + */ + public function indexAction($page) + { + // ... + } + .. code-block:: yaml # app/config/routing.yml @@ -449,10 +532,10 @@ URL route parameters .. caution:: - Of course, you can have more than one optional placeholder (e.g. ``/blog/{slug}/{page}``), - but everything after an optional placeholder must be optional. For example, - ``/{page}/blog`` is a valid path, but ``page`` will always be required - (i.e. simply ``/blog`` will not match this route). + Of course, you can have more than one optional placeholder (e.g. + ``/blog/{slug}/{page}``), but everything after an optional placeholder must + be optional. For example, ``/{page}/blog`` is a valid path, but ``page`` + will always be required (i.e. simply ``/blog`` will not match this route). .. tip:: @@ -469,6 +552,30 @@ Take a quick look at the routes that have been created so far: .. configuration-block:: + .. code-block:: php-annotations + + // src/AppBundle/Controller/BlogController.php + + // ... + class BlogController extends Controller + { + /** + * @Route("/blog/{page}", defaults={"page" = 1}) + */ + public function indexAction($page) + { + // ... + } + + /** + * @Route("/blog/{slug}") + */ + public function showAction($slug) + { + // ... + } + } + .. code-block:: yaml # app/config/routing.yml @@ -539,6 +646,20 @@ requirements can easily be added for each parameter. For example: .. configuration-block:: + .. code-block:: php + + // src/AppBundle/Controller/BlogController.php + + // ... + + /** + * @Route("/blog/{page}", defaults={"page": 1}, requirements={"page": "\d+"}) + */ + public function indexAction($page) + { + // ... + } + .. code-block:: yaml # app/config/routing.yml @@ -614,14 +735,29 @@ URL: .. configuration-block:: + .. code-block:: php-annotations + + // src/AppBundle/Controller/MainController.php + + // ... + class MainController extends Controller + { + /** + * @Route("/{_locale}", defaults={"_locale": "en"}, requirements={"_locale": "en|fr"}) + */ + public function homepageAction($_locale) + { + } + } + .. code-block:: yaml # app/config/routing.yml homepage: - path: /{culture} - defaults: { _controller: AppBundle:Main:homepage, culture: en } + path: /{_locale} + defaults: { _controller: AppBundle:Main:homepage, _locale: en } requirements: - culture: en|fr + _locale: en|fr .. code-block:: xml @@ -632,10 +768,10 @@ URL: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + AppBundle:Main:homepage - en - en|fr + en + en|fr @@ -646,24 +782,24 @@ URL: use Symfony\Component\Routing\Route; $collection = new RouteCollection(); - $collection->add('homepage', new Route('/{culture}', array( + $collection->add('homepage', new Route('/{_locale}', array( '_controller' => 'AppBundle:Main:homepage', - 'culture' => 'en', + '_locale' => 'en', ), array( - 'culture' => 'en|fr', + '_locale' => 'en|fr', ))); return $collection; -For incoming requests, the ``{culture}`` portion of the URL is matched against +For incoming requests, the ``{_locale}`` portion of the URL is matched against the regular expression ``(en|fr)``. ======= ======================== path parameters ======= ======================== -``/`` {culture} = en -``/en`` {culture} = en -``/fr`` {culture} = fr +``/`` {_locale} = en +``/en`` {_locale} = en +``/fr`` {_locale} = fr ``/es`` *won't match this route* ======= ======================== @@ -681,6 +817,35 @@ be accomplished with the following route configuration: .. configuration-block:: + .. code-block:: php-annotations + + // src/AppBundle/Controller/MainController.php + namespace AppBundle\Controller; + + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; + // ... + + class MainController extends Controller + { + /** + * @Route("/contact") + * @Method("GET") + */ + public function contactAction() + { + // ... display contact form + } + + /** + * @Route("/contact") + * @Method("POST") + */ + public function processContactAction() + { + // ... process contact form + } + } + .. code-block:: yaml # app/config/routing.yml @@ -691,7 +856,7 @@ be accomplished with the following route configuration: contact_process: path: /contact - defaults: { _controller: AppBundle:Main:contactProcess } + defaults: { _controller: AppBundle:Main:processContact } methods: [POST] .. code-block:: xml @@ -708,7 +873,7 @@ be accomplished with the following route configuration: - AppBundle:Main:contactProcess + AppBundle:Main:processContact @@ -724,7 +889,7 @@ be accomplished with the following route configuration: ), array(), array(), '', array(), array('GET'))); $collection->add('contact_process', new Route('/contact', array( - '_controller' => 'AppBundle:Main:contactProcess', + '_controller' => 'AppBundle:Main:processContact', ), array(), array(), '', array(), array('POST'))); return $collection; @@ -767,14 +932,33 @@ routing system can be: .. configuration-block:: + .. code-block:: php-annotation + + // src/AppBundle/Controller/ArticleController.php + + // ... + class ArticleController extends Controller + { + /** + * @Route( + * "/articles/{_locale}/{year}/{title}.{_format}", + * defaults: {"_format": "html"} + * requirements: {"_locale": "en|fr", "_format": "html|rss", "year": "\d+"} + * ) + */ + public function showAction($_locale, $year, $title) + { + } + } + .. code-block:: yaml # app/config/routing.yml article_show: - path: /articles/{culture}/{year}/{title}.{_format} + path: /articles/{_locale}/{year}/{title}.{_format} defaults: { _controller: AppBundle:Article:show, _format: html } requirements: - culture: en|fr + _locale: en|fr _format: html|rss year: \d+ @@ -788,11 +972,11 @@ routing system can be: http://symfony.com/schema/routing/routing-1.0.xsd"> + path="/articles/{_locale}/{year}/{title}.{_format}"> AppBundle:Article:show html - en|fr + en|fr html|rss \d+ @@ -808,11 +992,11 @@ routing system can be: $collection = new RouteCollection(); $collection->add( 'article_show', - new Route('/articles/{culture}/{year}/{title}.{_format}', array( + new Route('/articles/{_locale}/{year}/{title}.{_format}', array( '_controller' => 'AppBundle:Article:show', '_format' => 'html', ), array( - 'culture' => 'en|fr', + '_locale' => 'en|fr', '_format' => 'html|rss', 'year' => '\d+', )) @@ -820,7 +1004,7 @@ routing system can be: return $collection; -As you've seen, this route will only match if the ``{culture}`` portion of +As you've seen, this route will only match if the ``{_locale}`` portion of the URL is either ``en`` or ``fr`` and if the ``{year}`` is a number. This route also shows how you can use a dot between placeholders instead of a slash. URLs matching this route might look like: @@ -937,11 +1121,12 @@ for a route parameter of that name and assigns its value to that argument. In the advanced example above, any combination (in any order) of the following variables could be used as arguments to the ``showAction()`` method: -* ``$culture`` +* ``$_locale`` * ``$year`` * ``$title`` * ``$_format`` * ``$_controller`` +* ``$_route`` Since the placeholders and ``defaults`` collection are merged together, even the ``$_controller`` variable is available. For a more detailed discussion, @@ -949,8 +1134,8 @@ see :ref:`route-parameters-controller-arguments`. .. tip:: - You can also use a special ``$_route`` variable, which is set to the - name of the route that was matched. + The special ``$_route`` variable is set to the name of the route that was + matced. You can even add extra information to your route definition and access it within your controller. For more information on this topic, @@ -1134,7 +1319,7 @@ your application: homepage ANY / contact GET /contact contact_process POST /contact - article_show ANY /articles/{culture}/{year}/{title}.{_format} + article_show ANY /articles/{_locale}/{year}/{title}.{_format} blog ANY /blog/{page} blog_show ANY /blog/{slug} From 51773f4d55656b25f26c319a641e839e23dedb7b Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 7 Nov 2014 11:00:13 +0100 Subject: [PATCH 13/15] Proofread routing article --- book/routing.rst | 103 +++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index 70036b605b6..6abad1e9247 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -1150,9 +1150,10 @@ Including External Routing Resources ------------------------------------ All routes are loaded via a single configuration file - usually -``app/config/routing.yml`` (see `Creating Routes`_ above). Commonly, however, -you'll want to load routes from other places, like a routing file that lives -inside a bundle. This can be done by "importing" that file: +``app/config/routing.yml`` (see `Creating Routes`_ above). However, if you use +routing annotations, you'll need to point the router to the controllers with +the annotations. This can be done by "importing" directories into the routing +configuration: .. configuration-block:: @@ -1160,7 +1161,8 @@ inside a bundle. This can be done by "importing" that file: # app/config/routing.yml app: - resource: "@AppBundle/Resources/config/routing.yml" + resource: "@AppBundle/Controller/" + type: annotation # required to enable the Annotation reader for this resource .. code-block:: xml @@ -1171,7 +1173,8 @@ inside a bundle. This can be done by "importing" that file: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + + .. code-block:: php @@ -1181,7 +1184,9 @@ inside a bundle. This can be done by "importing" that file: $collection = new RouteCollection(); $collection->addCollection( - $loader->import("@AppBundle/Resources/config/routing.php") + // second argument is the type, which is required to enable the annotation reader + // for this resource + $loader->import("@AppBundle/Controller/", "annotation") ); return $collection; @@ -1192,55 +1197,53 @@ inside a bundle. This can be done by "importing" that file: Just be sure that it's unique so no other lines override it. The ``resource`` key loads the given routing resource. In this example the -resource is the full path to a file, where the ``@AppBundle`` shortcut -syntax resolves to the path of that bundle. The imported file might look -like this: +resource is a directory, where the ``@AppBundle`` shortcut syntax resolves to +the full path of the AppBundle. When pointing to a directory, all files in that +directory are parsed and put into the routing. -.. configuration-block:: +.. note:: - .. code-block:: yaml + You can also include other routing configuration files, this is often used + to import the routing of third party bundles: - # src/AppBundle/Resources/config/routing.yml - app: - path: /hello/{name} - defaults: { _controller: AppBundle:Hello:index } + .. configuration-block:: - .. code-block:: xml + .. code-block:: yaml - - - + # app/config/routing.yml + app: + resource: "@AcmeOtherBundle/Resources/config/routing.yml" - - AppBundle:Hello:index - - + .. code-block:: xml - .. code-block:: php + + + - // src/AppBundle/Resources/config/routing.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + + - $collection = new RouteCollection(); - $collection->add('acme_hello', new Route('/hello/{name}', array( - '_controller' => 'AppBundle:Hello:index', - ))); + .. code-block:: php - return $collection; + // app/config/routing.php + use Symfony\Component\Routing\RouteCollection; -The routes from this file are parsed and loaded in the same way as the main -routing file. + $collection = new RouteCollection(); + $collection->addCollection( + $loader->import("@AcmeOtherBundle/Resources/config/routing.php") + ); + + return $collection; Prefixing Imported Routes ~~~~~~~~~~~~~~~~~~~~~~~~~ You can also choose to provide a "prefix" for the imported routes. For example, -suppose you want the ``app`` routes to have a final path of ``/admin/hello/{name}`` -instead of simply ``/hello/{name}``: +suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g. +``/site/blog/{slug}`` instead of ``/blog/{slug}``): .. configuration-block:: @@ -1248,8 +1251,9 @@ instead of simply ``/hello/{name}``: # app/config/routing.yml app: - resource: "@AppBundle/Resources/config/routing.yml" - prefix: /admin + resource: "@AppBundle/Controller/" + type: annotation + prefix: /site .. code-block:: xml @@ -1261,8 +1265,9 @@ instead of simply ``/hello/{name}``: http://symfony.com/schema/routing/routing-1.0.xsd"> + resource="@AppBundle/Controller/" + type="annotation" + prefix="/site" /> .. code-block:: php @@ -1270,23 +1275,17 @@ instead of simply ``/hello/{name}``: // app/config/routing.php use Symfony\Component\Routing\RouteCollection; - $app = $loader->import('@AppBundle/Resources/config/routing.php'); - $app->addPrefix('/admin'); + $app = $loader->import('@AppBundle/Controller/'); + $app->addPrefix('/site'); $collection = new RouteCollection(); $collection->addCollection($app); return $collection; -The string ``/admin`` will now be prepended to the path of each route loaded +The string ``/site`` will now be prepended to the path of each route loaded from the new routing resource. -.. tip:: - - You can also define routes using annotations. See the - :doc:`FrameworkExtraBundle documentation ` - to see how. - Adding a Host Requirement to Imported Routes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 9678b61cdf3dc14cf1f5995e2bd6352774a11414 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 7 Nov 2014 11:09:30 +0100 Subject: [PATCH 14/15] Proofread templating --- book/templating.rst | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/book/templating.rst b/book/templating.rst index 413c769b1bb..e5b7c5de7cc 100644 --- a/book/templating.rst +++ b/book/templating.rst @@ -388,12 +388,21 @@ By default, templates can live in two different locations: * ``path/to/bundle/Resources/views/``: Each third party bundle houses its templates in its ``Resources/views`` directory (and subdirectories). When you - plan to share your bundle, you should put the templates in the shared bundle - instead of the ``app/Resources/views`` directory. + plan to share your bundle, you should put the templates in the bundle instead + of the ``app/`` directory. + +Most of the templates you'll use live in the ``app/Resources/views/`` +directory. The path you'll use will be relative to this directory. For example, +to render/extend ``app/Resources/views/base.html.twig``, you'll use the +``base.html.twig`` path and to render/extend +``app/Resources/views/Blog/index.html.twig``, you'll use the +``Blog/index.html.twig`` path. + +Referencing Templates in a Bundle +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Symfony uses a **bundle**:**directory**:**filename** string syntax for -templates. You can also give paths, which are relative to the -``app/Resources/views`` directory. This allows for several different types of +templates that live inside a bundle. This allows for several different types of templates, each which lives in a specific location: * ``AcmeBlogBundle:Blog:index.html.twig``: This syntax is used to specify a @@ -419,15 +428,6 @@ templates, each which lives in a specific location: Yes, there are 2 colons in the middle of the string when the "controller" subdirectory part is missing. -* ``::base.html.twig``: This syntax refers to views in ``app/Resources/views``. - Notice that the string begins with two colons (``::``), meaning that both the - *bundle* and *directory* portions are missing. This means that the template - is not located in any bundle, but instead in the root - ``app/Resources/views/`` directory. - -* ``base.html.twig``: Equivalent to ``::base.html.twig`` and **recommended** - for application-wide templates. - In the :ref:`overriding-bundle-templates` section, you'll find out how each template living inside the ``AcmeBlogBundle``, for example, can be overridden by placing a template of the same name in the ``app/Resources/AcmeBlogBundle/views/`` @@ -435,8 +435,8 @@ directory. This gives the power to override templates from any vendor bundle. .. tip:: - Hopefully the template naming syntax looks familiar - it's the same naming - convention used to refer to :ref:`controller-string-syntax`. + Hopefully the template naming syntax looks familiar - it's similair to the + naming convention used to refer to :ref:`controller-string-syntax`. Template Suffix ~~~~~~~~~~~~~~~ @@ -1322,7 +1322,7 @@ covered: template is called ``base.html.twig``; * Create a template for each "section" of your site. For example, the blog - functionality would have a template called ``/Blog/layout.html.twig`` that + functionality would have a template called ``Blog/layout.html.twig`` that contains only blog section-specific elements; .. code-block:: html+jinja From e56c272cb231d554d27e544c0785e0547793d910 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 7 Nov 2014 11:14:40 +0100 Subject: [PATCH 15/15] Fixed error --- book/routing.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index 6abad1e9247..cab6efb7953 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -34,7 +34,7 @@ The route is simple: .. configuration-block:: - .. code-block:: php-annotation + .. code-block:: php-annotations // src/AppBundle/Controller/BlogController.php namespace AppBundle\Controller; @@ -213,7 +213,7 @@ A basic route consists of just two parts: the ``path`` to match and a .. configuration-block:: - .. code-block:: php-annotation + .. code-block:: php-annotations // src/AppBundle/Controller/MainController.php @@ -280,7 +280,7 @@ routes will contain one or more named "wildcard" placeholders: .. configuration-block:: - .. code-block:: php-annotation + .. code-block:: php-annotations // src/AppBundle/Controller/BlogController.php @@ -348,7 +348,7 @@ the available blog posts for this imaginary blog application: .. configuration-block:: - .. code-block:: php-annotation + .. code-block:: php-annotations // src/AppBundle/Controller/BlogController.php @@ -932,7 +932,7 @@ routing system can be: .. configuration-block:: - .. code-block:: php-annotation + .. code-block:: php-annotations // src/AppBundle/Controller/ArticleController.php