Symfony2 first stable release is scheduled for the first week of March 2011. During this session, we will have a look at the new framework architecture and most of its powerfull features.
We will show you how Symfony2 implements the MVC pattern and an HTTP request is processed and converted as a response for the end user. Of course, we will talk about the configuration principles and how it's easy to configure Symfony2 project parts like the routing system or the data model. We will also focus on other major components of the framework like the Doctrine2 integration, forms, security (authentication and authorizations) or HTTP cache management.
15. Symfony2 Philosophy
« Basically, Symfony2 asks you to
convert a Request into a Response »
16. Request handling
class DefaultController extends Controller
{
/**
* @extra:Route("/hello/{name}")
*/
public function indexAction($name)
{
// ... do things
return new Response(sprintf('Hello %s!', $name));
}
}
17. Request handling
class DefaultController extends Controller
{
/**
* @extra:Route("/hello/{name}")
*/
public function indexAction($name)
{
// ... do things
return $this->render('HelloBundle:Default:index.html.twig',
array('name' => $name));
}
}
18. Request handling
class DefaultController extends Controller
{
/**
* @extra:Route("/schedule")
* @extra:Template
*/
public function indexAction()
{
$title = 'Confoo 2011 Conferences Schedule';
return array('title' => $title);
}
}
19. Templating
{% extends "ConfooConferenceBundle::layout.html.twig" %}
{% block content %}
<h1> {{ title }} </h1>
<ul>
<li>Caching on the Edge, by Fabien Potencier</li>
<li>HipHop for PHP, by Scott Mac Vicar</li>
<li>XDebug, by Derick Rethans</li>
<li>...</li>
</ul>
{% endblock %}
20. TWIG Template Engine
Twig is a modern template engine for PHP
§ Fast
§ Concise and rich syntax
§ Automatic output escaping
§ Modern features
§ Extensible
§ Flexible
21. Template inheritance
{% extends "ConfooConferenceBundle::layout.html.twig" %}
{% block content %}
<h1> {{ title }} </h1>
<ul>
<li>Caching on the Edge, by Fabien Potencier</li>
<li>HipHop for PHP, by Scott Mac Vicar</li>
<li>XDebug, by Derick Rethans</li>
<li>...</li>
</ul>
{% endblock %}
44. Forms management
§ Transparent layer on top of your domain object
§ Native CSRF protection
§ Coupled to the Validation framework
§ Twig integration
45. Designing a basic form class
namespace ConfooContactBundleForm;
use SymfonyComponentFormForm;
use SymfonyComponentFormTextField;
use SymfonyComponentFormTextareaField;
use SymfonyComponentFormCheckboxField;
class ContactForm extends Form
{
protected function configure()
{
$this->add(new TextField('sender')));
$this->add(new TextareaField('message'));
}
}
46. Processing a form
public function contactAction()
{
$contactRequest = new ContactRequest();
$form = ContactForm::create(...);
$form->bind($this->get('request'), $contactRequest);
if ($form->isValid()) {
// do things with validated data
}
return array('form' => $form);
}