Symfony Framework
David Buchmann
Confoo Vancouver, December 6, 2016
David Buchmann
Twitter: @dbu
- Senior backend developer at L//P in Switzerland
- Symfony expert
- Maintainer of Symfony Content Management Framework (CMF)
- Other topics: HTTP and Varnish Cache, Agile
Outline
- What is Symfony?
- When to use Symfony
- Using Symfony
- Using Bundles
- Dependency Injection
Full HTTP framework with MVC integration and bundles
A RAD framework
A microframework
Base for Drupal 8, eZ publish, oroCRM, Silex, Laravel and other frameworks
When to use Symfony
Symfony Ecosystem
- Open Source, paid support available
- Strong, skilled community
- Symfony 1 started 2005
- Stable release cycle
https://symfony.com/doc/current/contributing/community/releases.html#schedule
Lets get technical!
Symfony full stack framework
Directory Layout
- app/ application configuration and bootstrapping
- bin/console for command line tools
- src/ your own PHP code
- var/ caches and logfiles, writable by the web server
- vendor/ all third party code, managed by composer
- web/ web root, application entry point and static files
Web
- Entry point is "hostname.lo/app.php"
- If you have mod_rewrite, there is a .htaccess to use "hostname.lo/"
- Debug entry point is app_dev.php
- Templates and configuration checked for changes and automatically re-compiled
- Debug toolbar at the bottom
- Different configuration per environment
Flow
- app.php creates Request and Kernel, calls kernel
- Kernel bootstraps framework
- Kernel triggers kernel.request event
- Built-in event listener determines controller based on request (Routing)
- Kernel calls controller
- Controller "does stuff" and returns Response
- app.php sends response
Controller
Controller
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function greetAction($name)
{
return new Response("Hello $name");
}
}
Routing: Map request to controller
# app/config/routing.yml
hello:
path: /hello/{name}
defaults:
_controller: AppBundle:Hello:greet
View
Symfony Twig integration
$this->render(
'AppBundle:Hello:greet.html.twig',
array('name' => $name)
);
src/AppBundle/Resources/Views/Hello/greet.html.twig
<!DOCTYPE html>
<html>
<head>
<title>Welcome to ConFoo!</title>
</head>
<body>
<h1>Hello: {{ name }}</h1>
</body>
</html>
Reuse: There is a bundle for that!
Find existing code
- PHP has lots of cool libraries
- Bundles integrate them into Symfony
- Fight the NIH syndrome
- Each bundle you can use is one less you need to write and maintain
- knpbundles.com
- packagist.org
Integrate KnpMarkdownBundle
composer require "knplabs/knp-markdown-bundle"
Add to app/AppKernel.php
$bundles = array(
// ...
new Knp\MarkdownBundle\KnpMarkdownBundle(),
);
Use it in template
{{ page.mainText|markdown }}
Lets talk about
Dependeny Injection
Dependeny Injection
class MyClass
{
public function doStuff()
{
global $repositoy;
return $repository->findStuff();
}
}
NO!
Dependeny Injection
class MyClass
{
public function doStuff()
{
return Registry::instance()
->get('repository')
->findStuff();
}
}
Meh. A bit better but not good.
Dependeny Injection
class MyClass
{
private $repository;
public function __construct($repository)
{
$this->repository = $repository;
}
public function doStuff()
{
return $this->repository->findStuff();
}
}
This is dependency injection!
Dependeny Injection
- Independant of any "service container"
- Push instead of pull
- Dependencies are explicit
- Reusable code
- Testable code
Symfony Dependeny Injection
- "service": lazy loaded class instance
- Define what other services need to be injected
- Service container only instantiates what is needed
- Controllers can be defined as service,
routes can refer to service names
Outlook
Lots of interesting components
Console, Finder, Config, Form and Validator, DomCrawler and CssSelector, EventDispatcher, Process
Major Version Changes
- Smooth upgrade path
- Last old minor version
- deprecates everything that will be removed
- and has all features of the new major version
Thank you!
Questions / Input / Feedback ?
@dbu