Skip to content

Commit

Permalink
Merge branch '2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Dec 6, 2012
2 parents 30d49c7 + a03f06f commit dccf123
Show file tree
Hide file tree
Showing 33 changed files with 376 additions and 143 deletions.
31 changes: 23 additions & 8 deletions book/controller.rst
Expand Up @@ -472,10 +472,13 @@ value to each variable.
object:: object::


$httpKernel = $this->container->get('http_kernel'); $httpKernel = $this->container->get('http_kernel');
$response = $httpKernel->forward('AcmeHelloBundle:Hello:fancy', array( $response = $httpKernel->forward(
'name' => $name, 'AcmeHelloBundle:Hello:fancy',
'color' => 'green', array(
)); 'name' => $name,
'color' => 'green',
)
);


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


$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name)); $content = $this->renderView(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);


return new Response($content); return new Response($content);


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


return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name)); return $this->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);


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


$templating = $this->get('templating'); $templating = $this->get('templating');
$content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name)); $content = $templating->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);


.. note:: .. note::


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


$templating->render('AcmeHelloBundle:Hello/Greetings:index.html.twig', array('name' => $name)); $templating->render(
'AcmeHelloBundle:Hello/Greetings:index.html.twig',
array('name' => $name)
);
// index.html.twig found in Resources/views/Hello/Greetings is rendered. // index.html.twig found in Resources/views/Hello/Greetings is rendered.


.. index:: .. index::
Expand Down
11 changes: 8 additions & 3 deletions book/doctrine.rst
Expand Up @@ -472,7 +472,9 @@ on its ``id`` value::
->find($id); ->find($id);


if (!$product) { if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id); throw $this->createNotFoundException(
'No product found for id '.$id
);
} }


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


if (!$product) { if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id); throw $this->createNotFoundException(
'No product found for id '.$id
);
} }


$product->setName('New product name!'); $product->setName('New product name!');
Expand Down Expand Up @@ -1308,7 +1312,8 @@ and ``nullable``. Take a few examples:
/** /**
* A string field with length 255 that cannot be null * A string field with length 255 that cannot be null
* (reflecting the default values for the "type", "length" and *nullable* options) * (reflecting the default values for the "type", "length"
* and *nullable* options)
* *
* @ORM\Column() * @ORM\Column()
*/ */
Expand Down
4 changes: 3 additions & 1 deletion book/forms.rst
Expand Up @@ -1565,7 +1565,9 @@ method to specify the option::
{ {
$collectionConstraint = new Collection(array( $collectionConstraint = new Collection(array(
'name' => new Length(array("min" => 5)), 'name' => new Length(array("min" => 5)),
'email' => new Email(array('message' => 'Invalid email address')), 'email' => new Email(
array('message' => 'Invalid email address')
),
)); ));


$resolver->setDefaults(array( $resolver->setDefaults(array(
Expand Down
20 changes: 16 additions & 4 deletions book/from_flat_php_to_symfony2.rst
Expand Up @@ -554,7 +554,10 @@ them for you. Here's the same sample application, now built in Symfony2::
->createQuery('SELECT p FROM AcmeBlogBundle:Post p') ->createQuery('SELECT p FROM AcmeBlogBundle:Post p')
->execute(); ->execute();


return $this->render('AcmeBlogBundle:Blog:list.html.php', array('posts' => $posts)); return $this->render(
'AcmeBlogBundle:Blog:list.html.php',
array('posts' => $posts)
);
} }


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


return $this->render('AcmeBlogBundle:Blog:show.html.php', array('post' => $post)); return $this->render(
'AcmeBlogBundle:Blog:show.html.php',
array('post' => $post)
);
} }
} }


Expand All @@ -590,7 +596,10 @@ now quite a bit simpler:
<ul> <ul>
<?php foreach ($posts as $post): ?> <?php foreach ($posts as $post): ?>
<li> <li>
<a href="<?php echo $view['router']->generate('blog_show', array('id' => $post->getId())) ?>"> <a href="<?php echo $view['router']->generate(
'blog_show',
array('id' => $post->getId())
) ?>">
<?php echo $post->getTitle() ?> <?php echo $post->getTitle() ?>
</a> </a>
</li> </li>
Expand All @@ -605,7 +614,10 @@ The layout is nearly identical:
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title><?php echo $view['slots']->output('title', 'Default title') ?></title> <title><?php echo $view['slots']->output(
'title',
'Default title'
) ?></title>
</head> </head>
<body> <body>
<?php echo $view['slots']->output('_content') ?> <?php echo $view['slots']->output('_content') ?>
Expand Down
14 changes: 11 additions & 3 deletions book/page_creation.rst
Expand Up @@ -288,10 +288,16 @@ of writing the HTML inside the controller, render a template instead:
{ {
public function indexAction($name) public function indexAction($name)
{ {
return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name)); return $this->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);
// render a PHP template instead // render a PHP template instead
// return $this->render('AcmeHelloBundle:Hello:index.html.php', array('name' => $name)); // return $this->render(
// 'AcmeHelloBundle:Hello:index.html.php',
// array('name' => $name)
// );
} }
} }
Expand Down Expand Up @@ -903,7 +909,9 @@ file of your choice::
// app/AppKernel.php // app/AppKernel.php
public function registerContainerConfiguration(LoaderInterface $loader) public function registerContainerConfiguration(LoaderInterface $loader)
{ {
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml'); $loader->load(
__DIR__.'/config/config_'.$this->getEnvironment().'.yml'
);
} }


You already know that the ``.yml`` extension can be changed to ``.xml`` or You already know that the ``.yml`` extension can be changed to ``.xml`` or
Expand Down
2 changes: 1 addition & 1 deletion book/performance.rst
Expand Up @@ -56,7 +56,7 @@ finally finds the file it's looking for.


The simplest solution is to tell Composer to build a "class map" (i.e. a The simplest solution is to tell Composer to build a "class map" (i.e. a
big array of the locations of all the classes). This can be done from the big array of the locations of all the classes). This can be done from the
command line, and might become part of your deploy process:: command line, and might become part of your deploy process:


.. code-block:: bash .. code-block:: bash
Expand Down
8 changes: 6 additions & 2 deletions book/propel.rst
Expand Up @@ -181,7 +181,9 @@ value::
->findPk($id); ->findPk($id);


if (!$product) { if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id); throw $this->createNotFoundException(
'No product found for id '.$id
);
} }


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


if (!$product) { if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id); throw $this->createNotFoundException(
'No product found for id '.$id
);
} }


$product->setName('New product name!'); $product->setName('New product name!');
Expand Down
25 changes: 20 additions & 5 deletions book/routing.rst
Expand Up @@ -417,7 +417,7 @@ match, giving the ``page`` parameter a value of ``2``. Perfect.


.. tip:: .. tip::


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


.. index:: .. index::
Expand Down Expand Up @@ -787,6 +787,12 @@ a slash. URLs matching this route might look like:
each value of ``_format``. The ``_format`` parameter is a very powerful way each value of ``_format``. The ``_format`` parameter is a very powerful way
to render the same content in different formats. to render the same content in different formats.


.. note::

Sometimes you want to make certain parts of your routes globally configurable.
Symfony2.1 provides you with a way to do this by leveraging service container
parameters. Read more about this in ":doc:`/cookbook/routing/service_container_parameters`.

Special Routing Parameters Special Routing Parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~


Expand Down Expand Up @@ -1103,7 +1109,10 @@ a route+parameters back to a URL. The
system. Take the ``blog_show`` example route from earlier:: system. Take the ``blog_show`` example route from earlier::


$params = $router->match('/blog/my-blog-post'); $params = $router->match('/blog/my-blog-post');
// array('slug' => 'my-blog-post', '_controller' => 'AcmeBlogBundle:Blog:show') // array(
// 'slug' => 'my-blog-post',
// '_controller' => 'AcmeBlogBundle:Blog:show',
// )


$uri = $router->generate('blog_show', array('slug' => 'my-blog-post')); $uri = $router->generate('blog_show', array('slug' => 'my-blog-post'));
// /blog/my-blog-post // /blog/my-blog-post
Expand All @@ -1116,9 +1125,12 @@ that route. With this information, any URL can easily be generated::
{ {
public function showAction($slug) public function showAction($slug)
{ {
// ... // ...


$url = $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post')); $url = $this->get('router')->generate(
'blog_show',
array('slug' => 'my-blog-post')
);
} }
} }


Expand All @@ -1132,7 +1144,10 @@ In an upcoming section, you'll learn how to generate URLs from inside templates.


.. code-block:: javascript .. code-block:: javascript
var url = Routing.generate('blog_show', { "slug": 'my-blog-post'}); var url = Routing.generate(
'blog_show',
{"slug": 'my-blog-post'}
);
For more information, see the documentation for that bundle. For more information, see the documentation for that bundle.


Expand Down
17 changes: 11 additions & 6 deletions book/security.rst
Expand Up @@ -434,17 +434,22 @@ Next, create the controller that will display the login form::


// get the login error if there is one // get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); $error = $request->attributes->get(
SecurityContext::AUTHENTICATION_ERROR
);
} else { } else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR); $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR); $session->remove(SecurityContext::AUTHENTICATION_ERROR);
} }


return $this->render('AcmeSecurityBundle:Security:login.html.twig', array( return $this->render(
// last username entered by the user 'AcmeSecurityBundle:Security:login.html.twig',
'last_username' => $session->get(SecurityContext::LAST_USERNAME), array(
'error' => $error, // last username entered by the user
)); 'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
)
);
} }
} }


Expand Down
26 changes: 21 additions & 5 deletions book/service_container.rst
Expand Up @@ -565,10 +565,14 @@ something like this::
Without using the service container, you can create a new ``NewsletterManager`` Without using the service container, you can create a new ``NewsletterManager``
fairly easily from inside a controller:: fairly easily from inside a controller::


use Acme\HelloBundle\Newsletter\NewsletterManager;

// ...

public function sendNewsletterAction() public function sendNewsletterAction()
{ {
$mailer = $this->get('my_mailer'); $mailer = $this->get('my_mailer');
$newsletter = new Acme\HelloBundle\Newsletter\NewsletterManager($mailer); $newsletter = new NewsletterManager($mailer);
// ... // ...
} }


Expand Down Expand Up @@ -618,7 +622,10 @@ the service container gives you a much more appealing option:
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
// ... // ...
$container->setParameter('newsletter_manager.class', 'Acme\HelloBundle\Newsletter\NewsletterManager'); $container->setParameter(
'newsletter_manager.class',
'Acme\HelloBundle\Newsletter\NewsletterManager'
);
$container->setDefinition('my_mailer', ...); $container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition( $container->setDefinition('newsletter_manager', new Definition(
Expand Down Expand Up @@ -708,7 +715,10 @@ Injecting the dependency by the setter method just needs a change of syntax:
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
// ... // ...
$container->setParameter('newsletter_manager.class', 'Acme\HelloBundle\Newsletter\NewsletterManager'); $container->setParameter(
'newsletter_manager.class',
'Acme\HelloBundle\Newsletter\NewsletterManager'
);
$container->setDefinition('my_mailer', ...); $container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition( $container->setDefinition('newsletter_manager', new Definition(
Expand Down Expand Up @@ -767,12 +777,18 @@ it exists and do nothing if it doesn't:
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
// ... // ...
$container->setParameter('newsletter_manager.class', 'Acme\HelloBundle\Newsletter\NewsletterManager'); $container->setParameter(
'newsletter_manager.class',
'Acme\HelloBundle\Newsletter\NewsletterManager'
);
$container->setDefinition('my_mailer', ...); $container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition( $container->setDefinition('newsletter_manager', new Definition(
'%newsletter_manager.class%', '%newsletter_manager.class%',
array(new Reference('my_mailer', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)) array(new Reference(
'my_mailer',
ContainerInterface::IGNORE_ON_INVALID_REFERENCE
))
)); ));
In YAML, the special ``@?`` syntax tells the service container that the dependency In YAML, the special ``@?`` syntax tells the service container that the dependency
Expand Down

0 comments on commit dccf123

Please sign in to comment.