Michał Pipa
Symfony2 is a reusable set of standalone, decoupled, and cohesive PHP components that solve common web development problems.
Symfony2 is also a full-stack web framework.
Symfony2 is an HTTP framework; it is a Request/Response framework.
3rd party libraries integration
Plugin, just fancy name
<?php
class Newsletter
{
private $mailer;
public function __construct()
{
$this->mailer = new MyMailer();
}
}
<?php
$newsletter = new Newsletter();
<?php
interface Mailer
{
public function send();
}
<?php
class Newsletter
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}
<?php
$mailer = new MyMailer();
$newsletter = new Newsletter($mailer);
<?php
class Newsletter
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}
<?php
$mailer = new FakeMailer();
$newsletter = new Newsletter($mailer);
# app/config/config.yml
services:
mailer:
class: MyMailer
newsletter:
class: Newsletter
arguments: ["%mailer%"]
<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;
$container = new ContainerBuilder();
// ...
$newsletter = $container->get('newsletter');
Provides tools that allow your application components to communicate with each other by dispatching events and listening to them.
<?php
use Symfony\Component\EventDispatcher\EventDispatcher;
$dispatcher = new EventDispatcher();
<?php
$event = new Event();
$dispatcher->dispatch('order.create', $event);
<?php
class OrderListener
{
public function onCreate(Event $event)
{
// do something
}
}
<?php
$listener = new OrderListener();
$dispatcher->addListener('order.create', array($listener, 'onCreate'));
# app/config/config.yml
services:
kernel.listener.order_listener:
class: OrderListener
tags:
- { name: kernel.event_listener, event: order.create, method: onCreate }
The HttpFoundation component defines an object-oriented layer for the HTTP specification.
The HttpKernel component provides a structured process for converting a Request into a Response by making use of the EventDispatcher.
<?php
namespace Symfony\Component\HttpKernel;
use Symfony\Component\HttpFoundation\Request;
interface HttpKernelInterface
{
/**
* @return Response A Response instance
*/
public function handle(
Request $request,
$type = self::MASTER_REQUEST,
$catch = true
);
}
Typical Purposes: To add more information to the Request, initialize parts of the system, or return a Response if possible (e.g. a security layer that denies access).
<?php
namespace Symfony\Component\HttpKernel\Controller;
use Symfony\Component\HttpFoundation\Request;
interface ControllerResolverInterface
{
public function getController(Request $request);
public function getArguments(Request $request, $controller);
}
Typical Purposes: Initialize things or change the controller just before the controller is executed.
Typical Purposes: Transform a non-Response return value from a controller into a Response
Typical Purposes: Modify the Response object just before it is sent
Typical Purposes: To perform some "heavy" action after the response has been streamed to the user
<?php
// send the headers and echo the content
$response->send();
// triggers the kernel.terminate event
$kernel->terminate($request, $response);
Typical Purposes: Handle some type of exception and create an appropriate Response to return for the exception