Skip to content

Commit 22c3614

Browse files
committed
Updating 2 more articles for 4.0
1 parent 80aceb0 commit 22c3614

File tree

2 files changed

+58
-107
lines changed

2 files changed

+58
-107
lines changed

contributing/code/reproducer.rst

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ Reproducing Complex Bugs
3333
------------------------
3434

3535
If the bug is related to the Symfony Framework or if it's too complex to create
36-
a PHP script, it's better to reproduce the bug by forking the Symfony Standard
37-
edition. To do so:
38-
39-
#. Go to https://github.com/symfony/symfony-standard and click on the **Fork**
40-
button to make a fork of that repository or go to your already forked copy.
41-
#. Clone the forked repository into your computer:
42-
``git clone git://github.com/YOUR-GITHUB-USERNAME/symfony-standard.git``
43-
#. Browse the project and create a new branch (e.g. ``issue_23567``,
44-
``reproduce_23657``, etc.)
45-
#. Now you must add the minimum amount of code to reproduce the bug. This is the
36+
a PHP script, it's better to reproduce the bug by creating a new project. To do so:
37+
38+
1. Create a new project:
39+
40+
.. code-block:: terminal
41+
42+
$ composer require symfony/skeleton bug_app
43+
44+
2. Now you must add the minimum amount of code to reproduce the bug. This is the
4645
trickiest part and it's explained a bit more later.
47-
#. Add, commit and push all your changes.
48-
#. Add a comment in your original issue report to share the URL of your forked
49-
project (e.g. ``https://github.com/YOUR-GITHUB-USERNAME/symfony-standard/tree/issue_23567``)
46+
3. Add and commit your changes.
47+
4. Create a `new repository`_ on GitHub (give it any name).
48+
5. Follow the instructions on GitHub to add the ``origin`` remote to your local project
49+
and push it.
50+
6. Add a comment in your original issue report to share the URL of your forked
51+
project (e.g. ``https://github.com/YOUR-GITHUB-USERNAME/symfony_issue_23567``)
5052
and, if necessary, explain the steps to reproduce (e.g. "browse this URL",
5153
"fill in this data in the form and submit it", etc.)
5254

@@ -55,23 +57,24 @@ Adding the Minimum Amount of Code Possible
5557

5658
The key to create a bug reproducer is to solely focus on the feature that you
5759
suspect is failing. For example, imagine that you suspect that the bug is related
58-
to a route definition. Then, after forking the Symfony Standard Edition:
60+
to a route definition. Then, after creating your project:
5961

6062
#. Don't edit any of the default Symfony configuration options.
6163
#. Don't copy your original application code and don't use the same structure
62-
of bundles, controllers, actions, etc. as in your original application.
63-
#. Open the default controller class of the AppBundle and add your routing
64-
definition using annotations.
64+
of controllers, actions, etc. as in your original application.
65+
#. Create a simple controller and add your routing definition that shows the bug.
6566
#. Don't create or modify any other file.
66-
#. Execute the ``server:run`` command and browse the previously defined route
67-
to see if the bug appears or not.
67+
#. Execute ``composer require server`` and use the ``server:run`` command to browse
68+
to the new route and see if the bug appears or not.
6869
#. If you can see the bug, you're done and you can already share the code with us.
6970
#. If you can't see the bug, you must keep making small changes. For example, if
7071
your original route was defined using XML, forget about the previous route
71-
annotation and define the route using XML instead. Or maybe your application
72-
uses bundle inheritance and that's where the real bug is. Then, forget about
73-
AppBundle and quickly generate a new AppParentBundle, make AppBundle inherit
74-
from it and test if the route is working.
72+
and define the route using XML instead. Or maybe your application
73+
registers some event listeners and that's where the real bug is. In that case,
74+
add an event listener that's similar to your real app to see if you can find
75+
the bug.
76+
77+
In short, the idea is to keep adding small and incremental changes to a new project
78+
until you can reproduce the bug.
7579

76-
In short, the idea is to keep adding small and incremental changes to the default
77-
Symfony Standard edition until you can reproduce the bug.
80+
.. _`new repository`: https://github.com/new

introduction/from_flat_php_to_symfony2.rst

Lines changed: 30 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ content:
431431
432432
{
433433
"require": {
434-
"symfony/symfony": "3.1.*"
434+
"symfony/http-foundation": "^4.0"
435435
},
436436
"autoload": {
437437
"files": ["model.php","controllers.php"]
@@ -534,9 +534,8 @@ a simple application. Along the way, you've made a simple routing
534534
system and a method using ``ob_start()`` and ``ob_get_clean()`` to render
535535
templates. If, for some reason, you needed to continue building this "framework"
536536
from scratch, you could at least use Symfony's standalone
537-
:doc:`Routing </components/routing>` and
538-
:doc:`Templating </components/templating>` components, which already
539-
solve these problems.
537+
:doc:`Routing </components/routing>` and component and :doc:`Twig </templating>`,
538+
which already solve these problems.
540539

541540
Instead of re-solving common problems, you can let Symfony take care of
542541
them for you. Here's the same sample application, now built in Symfony::
@@ -549,17 +548,16 @@ them for you. Here's the same sample application, now built in Symfony::
549548

550549
class BlogController extends Controller
551550
{
552-
public function listAction()
551+
public function list()
553552
{
554553
$posts = $this->getDoctrine()
555-
->getManager()
556-
->createQuery('SELECT p FROM App:Post p')
557-
->execute();
554+
->getRepository(Post::class)
555+
->findAll()
558556

559-
return $this->render('Blog/list.html.php', array('posts' => $posts));
557+
return $this->render('blog/list.html.twig', ['posts' => $posts]);
560558
}
561559

562-
public function showAction($id)
560+
public function show($id)
563561
{
564562
$post = $this->getDoctrine()
565563
->getRepository(Post::class)
@@ -570,7 +568,7 @@ them for you. Here's the same sample application, now built in Symfony::
570568
throw $this->createNotFoundException();
571569
}
572570

573-
return $this->render('Blog/show.html.php', array('post' => $post));
571+
return $this->render('blog/show.html.php', ['post' => $post]);
574572
}
575573
}
576574

@@ -581,51 +579,49 @@ nice way to group related pages. The controller functions are also sometimes cal
581579
The two controllers (or actions) are still lightweight. Each uses the
582580
:doc:`Doctrine ORM library </doctrine>` to retrieve objects from the
583581
database and the Templating component to render a template and return a
584-
``Response`` object. The list ``list.php`` template is now quite a bit simpler:
582+
``Response`` object. The list ``list.html.twig`` template is now quite a bit simpler,
583+
and uses Twig:
585584

586-
.. code-block:: html+php
585+
.. code-block:: html+twig
587586

588-
<!-- templates/Blog/list.html.php -->
589-
<?php $view->extend('layout.html.php') ?>
587+
<!-- templates/blog/list.html.twig -->
588+
{% extends 'base.html.twig' %}
590589

591-
<?php $view['slots']->set('title', 'List of Posts') ?>
590+
{% block title %}List of Posts{% endblock %}
592591

593592
<h1>List of Posts</h1>
594593
<ul>
595-
<?php foreach ($posts as $post): ?>
594+
{% for post in posts %}
596595
<li>
597-
<a href="<?php echo $view['router']->path(
598-
'blog_show',
599-
array('id' => $post->getId())
600-
) ?>">
601-
<?= $post->getTitle() ?>
596+
<a href="{{ path('blog_show', { id: post.id }) }}">
597+
{{ post.title }}
602598
</a>
603599
</li>
604-
<?php endforeach ?>
600+
{% endfor %}
605601
</ul>
606602

607603
The ``layout.php`` file is nearly identical:
608604

609-
.. code-block:: html+php
605+
.. code-block:: html+twig
610606

611-
<!-- templates/layout.html.php -->
607+
<!-- templates/base.html.twig -->
612608
<!DOCTYPE html>
613609
<html>
614610
<head>
615-
<title><?= $view['slots']->output(
616-
'title',
617-
'Default title'
618-
) ?></title>
611+
<meta charset="UTF-8">
612+
<title>{% block title %}Welcome!{% endblock %}</title>
613+
{% block stylesheets %}{% endblock %}
619614
</head>
620615
<body>
621-
<?= $view['slots']->output('_content') ?>
616+
{% block body %}{% endblock %}
617+
{% block javascripts %}{% endblock %}
622618
</body>
623619
</html>
624620

625621
.. note::
626622

627-
The show ``show.php`` template is left as an exercise: updating it should be
628-
really similar to updating the ``list.php`` template.
623+
The ``show.html.twig`` template is left as an exercise: updating it should be
624+
really similar to updating the ``list.html.twig`` template.
629625

630626
When Symfony's engine (called the Kernel) boots up, it needs a map so
631627
that it knows which controllers to execute based on the request information.
@@ -637,11 +633,11 @@ in a readable format:
637633
# config/routes.yaml
638634
blog_list:
639635
path: /blog
640-
defaults: { _controller: AppBundle:Blog:list }
636+
controller: App\Controller\BlogController::list
641637
642638
blog_show:
643639
path: /blog/show/{id}
644-
defaults: { _controller: AppBundle:Blog:show }
640+
controller: App\Controller\BlogController::show
645641
646642
Now that Symfony is handling all the mundane tasks, the front controller
647643
``public/index.php`` is dead simple. And since it does so little, you'll never
@@ -671,54 +667,6 @@ It's a beautiful thing.
671667
:align: center
672668
:alt: Symfony request flow
673669

674-
Better Templates
675-
~~~~~~~~~~~~~~~~
676-
677-
If you choose to use it, Symfony comes standard with a templating engine
678-
called `Twig`_ that makes templates faster to write and easier to read.
679-
It means that the sample application could contain even less code! Take,
680-
for example, rewriting ``list.html.php`` template in Twig would look like
681-
this:
682-
683-
.. code-block:: html+twig
684-
685-
{# templates/blog/list.html.twig #}
686-
{% extends "layout.html.twig" %}
687-
688-
{% block title %}List of Posts{% endblock %}
689-
690-
{% block body %}
691-
<h1>List of Posts</h1>
692-
<ul>
693-
{% for post in posts %}
694-
<li>
695-
<a href="{{ path('blog_show', {'id': post.id}) }}">
696-
{{ post.title }}
697-
</a>
698-
</li>
699-
{% endfor %}
700-
</ul>
701-
{% endblock %}
702-
703-
And rewriting ``layout.html.php`` template in Twig would look like this:
704-
705-
.. code-block:: html+twig
706-
707-
{# templates/layout.html.twig #}
708-
<!DOCTYPE html>
709-
<html>
710-
<head>
711-
<title>{% block title %}Default title{% endblock %}</title>
712-
</head>
713-
<body>
714-
{% block body %}{% endblock %}
715-
</body>
716-
</html>
717-
718-
Twig is well-supported in Symfony. And while PHP templates will always
719-
be supported in Symfony, the many advantages of Twig will continue to
720-
be discussed. For more information, see the :doc:`templating article </templating>`.
721-
722670
Where Symfony Delivers
723671
----------------------
724672

0 commit comments

Comments
 (0)