Skip to content

Commit e1a2b67

Browse files
committed
Merge pull request #1975 from richardmiller/line_lengths_book
Reducing code sample line lengths to avoid horizontal scrolling
2 parents dcfdfa3 + 36de753 commit e1a2b67

13 files changed

+198
-64
lines changed

book/controller.rst

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,13 @@ value to each variable.
472472
object::
473473

474474
$httpKernel = $this->container->get('http_kernel');
475-
$response = $httpKernel->forward('AcmeHelloBundle:Hello:fancy', array(
476-
'name' => $name,
477-
'color' => 'green',
478-
));
475+
$response = $httpKernel->forward(
476+
'AcmeHelloBundle:Hello:fancy',
477+
array(
478+
'name' => $name,
479+
'color' => 'green',
480+
)
481+
);
479482

480483
.. index::
481484
single: Controller; Rendering templates
@@ -490,14 +493,20 @@ that's responsible for generating the HTML (or other format) for the controller.
490493
The ``renderView()`` method renders a template and returns its content. The
491494
content from the template can be used to create a ``Response`` object::
492495

493-
$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
496+
$content = $this->renderView(
497+
'AcmeHelloBundle:Hello:index.html.twig',
498+
array('name' => $name)
499+
);
494500

495501
return new Response($content);
496502

497503
This can even be done in just one step with the ``render()`` method, which
498504
returns a ``Response`` object containing the content from the template::
499505

500-
return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
506+
return $this->render(
507+
'AcmeHelloBundle:Hello:index.html.twig',
508+
array('name' => $name)
509+
);
501510

502511
In both cases, the ``Resources/views/Hello/index.html.twig`` template inside
503512
the ``AcmeHelloBundle`` will be rendered.
@@ -517,15 +526,21 @@ The Symfony templating engine is explained in great detail in the
517526
service. The ``templating`` service can also be used directly::
518527

519528
$templating = $this->get('templating');
520-
$content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
529+
$content = $templating->render(
530+
'AcmeHelloBundle:Hello:index.html.twig',
531+
array('name' => $name)
532+
);
521533

522534
.. note::
523535

524536
It is possible to render templates in deeper subdirectories as well, however
525537
be careful to avoid the pitfall of making your directory structure unduly
526538
elaborate::
527539

528-
$templating->render('AcmeHelloBundle:Hello/Greetings:index.html.twig', array('name' => $name));
540+
$templating->render(
541+
'AcmeHelloBundle:Hello/Greetings:index.html.twig',
542+
array('name' => $name)
543+
);
529544
// index.html.twig found in Resources/views/Hello/Greetings is rendered.
530545

531546
.. index::
@@ -642,7 +657,10 @@ For example, imagine you're processing a form submit::
642657
if ($form->isValid()) {
643658
// do some sort of processing
644659

645-
$this->get('session')->setFlash('notice', 'Your changes were saved!');
660+
$this->get('session')->setFlash(
661+
'notice',
662+
'Your changes were saved!'
663+
);
646664

647665
return $this->redirect($this->generateUrl(...));
648666
}

book/doctrine.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ on its ``id`` value::
470470
->find($id);
471471

472472
if (!$product) {
473-
throw $this->createNotFoundException('No product found for id '.$id);
473+
throw $this->createNotFoundException(
474+
'No product found for id '.$id
475+
);
474476
}
475477

476478
// ... do something, like pass the $product object into a template
@@ -555,7 +557,9 @@ you have a route that maps a product id to an update action in a controller::
555557
$product = $em->getRepository('AcmeStoreBundle:Product')->find($id);
556558

557559
if (!$product) {
558-
throw $this->createNotFoundException('No product found for id '.$id);
560+
throw $this->createNotFoundException(
561+
'No product found for id '.$id
562+
);
559563
}
560564

561565
$product->setName('New product name!');
@@ -1306,7 +1310,8 @@ and ``nullable``. Take a few examples:
13061310
13071311
/**
13081312
* A string field with length 255 that cannot be null
1309-
* (reflecting the default values for the "type", "length" and *nullable* options)
1313+
* (reflecting the default values for the "type", "length"
1314+
* and *nullable* options)
13101315
*
13111316
* @ORM\Column()
13121317
*/

book/forms.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,9 @@ method to specify the option::
14771477
{
14781478
$collectionConstraint = new Collection(array(
14791479
'name' => new MinLength(5),
1480-
'email' => new Email(array('message' => 'Invalid email address')),
1480+
'email' => new Email(
1481+
array('message' => 'Invalid email address')
1482+
),
14811483
));
14821484

14831485
return array('validation_constraint' => $collectionConstraint);

book/from_flat_php_to_symfony2.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,10 @@ them for you. Here's the same sample application, now built in Symfony2::
554554
->createQuery('SELECT p FROM AcmeBlogBundle:Post p')
555555
->execute();
556556

557-
return $this->render('AcmeBlogBundle:Blog:list.html.php', array('posts' => $posts));
557+
return $this->render(
558+
'AcmeBlogBundle:Blog:list.html.php',
559+
array('posts' => $posts)
560+
);
558561
}
559562

560563
public function showAction($id)
@@ -570,7 +573,10 @@ them for you. Here's the same sample application, now built in Symfony2::
570573
throw $this->createNotFoundException();
571574
}
572575

573-
return $this->render('AcmeBlogBundle:Blog:show.html.php', array('post' => $post));
576+
return $this->render(
577+
'AcmeBlogBundle:Blog:show.html.php',
578+
array('post' => $post)
579+
);
574580
}
575581
}
576582

@@ -590,7 +596,10 @@ now quite a bit simpler:
590596
<ul>
591597
<?php foreach ($posts as $post): ?>
592598
<li>
593-
<a href="<?php echo $view['router']->generate('blog_show', array('id' => $post->getId())) ?>">
599+
<a href="<?php echo $view['router']->generate(
600+
'blog_show',
601+
array('id' => $post->getId())
602+
) ?>">
594603
<?php echo $post->getTitle() ?>
595604
</a>
596605
</li>
@@ -605,7 +614,10 @@ The layout is nearly identical:
605614
<!DOCTYPE html>
606615
<html>
607616
<head>
608-
<title><?php echo $view['slots']->output('title', 'Default title') ?></title>
617+
<title><?php echo $view['slots']->output(
618+
'title',
619+
'Default title'
620+
) ?></title>
609621
</head>
610622
<body>
611623
<?php echo $view['slots']->output('_content') ?>

book/page_creation.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,16 @@ of writing the HTML inside the controller, render a template instead:
288288
{
289289
public function indexAction($name)
290290
{
291-
return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
291+
return $this->render(
292+
'AcmeHelloBundle:Hello:index.html.twig',
293+
array('name' => $name)
294+
);
292295
293296
// render a PHP template instead
294-
// return $this->render('AcmeHelloBundle:Hello:index.html.php', array('name' => $name));
297+
// return $this->render(
298+
// 'AcmeHelloBundle:Hello:index.html.php',
299+
// array('name' => $name)
300+
// );
295301
}
296302
}
297303
@@ -900,7 +906,9 @@ file of your choice::
900906
// app/AppKernel.php
901907
public function registerContainerConfiguration(LoaderInterface $loader)
902908
{
903-
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
909+
$loader->load(
910+
__DIR__.'/config/config_'.$this->getEnvironment().'.yml'
911+
);
904912
}
905913

906914
You already know that the ``.yml`` extension can be changed to ``.xml`` or

book/propel.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ value::
181181
->findPk($id);
182182

183183
if (!$product) {
184-
throw $this->createNotFoundException('No product found for id '.$id);
184+
throw $this->createNotFoundException(
185+
'No product found for id '.$id
186+
);
185187
}
186188

187189
// ... do something, like pass the $product object into a template
@@ -202,7 +204,9 @@ have a route that maps a product id to an update action in a controller::
202204
->findPk($id);
203205

204206
if (!$product) {
205-
throw $this->createNotFoundException('No product found for id '.$id);
207+
throw $this->createNotFoundException(
208+
'No product found for id '.$id
209+
);
206210
}
207211

208212
$product->setName('New product name!');

book/routing.rst

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ match, giving the ``page`` parameter a value of ``2``. Perfect.
417417

418418
.. tip::
419419

420-
Routes with optional parameters at the end will not match on requests
420+
Routes with optional parameters at the end will not match on requests
421421
with a trailing slash (i.e. ``/blog/`` will not match, ``/blog`` will match).
422422

423423
.. index::
@@ -1068,7 +1068,10 @@ a route+parameters back to a URL. The
10681068
system. Take the ``blog_show`` example route from earlier::
10691069

10701070
$params = $router->match('/blog/my-blog-post');
1071-
// array('slug' => 'my-blog-post', '_controller' => 'AcmeBlogBundle:Blog:show')
1071+
// array(
1072+
// 'slug' => 'my-blog-post',
1073+
// '_controller' => 'AcmeBlogBundle:Blog:show',
1074+
// )
10721075

10731076
$uri = $router->generate('blog_show', array('slug' => 'my-blog-post'));
10741077
// /blog/my-blog-post
@@ -1081,9 +1084,12 @@ that route. With this information, any URL can easily be generated::
10811084
{
10821085
public function showAction($slug)
10831086
{
1084-
// ...
1087+
// ...
10851088

1086-
$url = $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post'));
1089+
$url = $this->get('router')->generate(
1090+
'blog_show',
1091+
array('slug' => 'my-blog-post')
1092+
);
10871093
}
10881094
}
10891095

@@ -1097,7 +1103,10 @@ In an upcoming section, you'll learn how to generate URLs from inside templates.
10971103

10981104
.. code-block:: javascript
10991105
1100-
var url = Routing.generate('blog_show', { "slug": 'my-blog-post'});
1106+
var url = Routing.generate(
1107+
'blog_show',
1108+
{"slug": 'my-blog-post'}
1109+
);
11011110
11021111
For more information, see the documentation for that bundle.
11031112

book/security.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,25 @@ Next, create the controller that will display the login form::
424424
$session = $request->getSession();
425425

426426
// get the login error if there is one
427-
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
428-
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
427+
if (
428+
$request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)
429+
) {
430+
$error = $request->attributes->get(
431+
SecurityContext::AUTHENTICATION_ERROR
432+
);
429433
} else {
430434
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
431435
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
432436
}
433437

434-
return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
435-
// last username entered by the user
436-
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
437-
'error' => $error,
438-
));
438+
return $this->render(
439+
'AcmeSecurityBundle:Security:login.html.twig',
440+
array(
441+
// last username entered by the user
442+
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
443+
'error' => $error,
444+
)
445+
);
439446
}
440447
}
441448

book/service_container.rst

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,14 @@ something like this::
567567
Without using the service container, you can create a new ``NewsletterManager``
568568
fairly easily from inside a controller::
569569

570+
use Acme\HelloBundle\Newsletter\NewsletterManager;
571+
572+
// ...
573+
570574
public function sendNewsletterAction()
571575
{
572576
$mailer = $this->get('my_mailer');
573-
$newsletter = new Acme\HelloBundle\Newsletter\NewsletterManager($mailer);
577+
$newsletter = new NewsletterManager($mailer);
574578
// ...
575579
}
576580

@@ -620,7 +624,10 @@ the service container gives you a much more appealing option:
620624
use Symfony\Component\DependencyInjection\Reference;
621625
622626
// ...
623-
$container->setParameter('newsletter_manager.class', 'Acme\HelloBundle\Newsletter\NewsletterManager');
627+
$container->setParameter(
628+
'newsletter_manager.class',
629+
'Acme\HelloBundle\Newsletter\NewsletterManager'
630+
);
624631
625632
$container->setDefinition('my_mailer', ...);
626633
$container->setDefinition('newsletter_manager', new Definition(
@@ -710,7 +717,10 @@ Injecting the dependency by the setter method just needs a change of syntax:
710717
use Symfony\Component\DependencyInjection\Reference;
711718
712719
// ...
713-
$container->setParameter('newsletter_manager.class', 'Acme\HelloBundle\Newsletter\NewsletterManager');
720+
$container->setParameter(
721+
'newsletter_manager.class',
722+
'Acme\HelloBundle\Newsletter\NewsletterManager'
723+
);
714724
715725
$container->setDefinition('my_mailer', ...);
716726
$container->setDefinition('newsletter_manager', new Definition(
@@ -769,12 +779,18 @@ it exists and do nothing if it doesn't:
769779
use Symfony\Component\DependencyInjection\ContainerInterface;
770780
771781
// ...
772-
$container->setParameter('newsletter_manager.class', 'Acme\HelloBundle\Newsletter\NewsletterManager');
782+
$container->setParameter(
783+
'newsletter_manager.class',
784+
'Acme\HelloBundle\Newsletter\NewsletterManager'
785+
);
773786
774787
$container->setDefinition('my_mailer', ...);
775788
$container->setDefinition('newsletter_manager', new Definition(
776789
'%newsletter_manager.class%',
777-
array(new Reference('my_mailer', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
790+
array(new Reference(
791+
'my_mailer',
792+
ContainerInterface::IGNORE_ON_INVALID_REFERENCE
793+
))
778794
));
779795
780796
In YAML, the special ``@?`` syntax tells the service container that the dependency

0 commit comments

Comments
 (0)