Rendering
$templating
=
new
PhpEngine(
new
TemplateNameParser(),
new
FilesystemLoader(
array(__DIR__
.
'/../templates/%name%')
)
);
$html
=
$templating-‐>render(
'list.php',
array(
'tasks'
=>
$tasks
)
);
Slide 45
Slide 45 text
Inside the Controller
public
function
listAction(Request
$request)
{
//
load
tasks
from
db
return
new
Response(
$this-‐>templating-‐>render(
'list.php',
array(
'urlGenerator'
=>
$router-‐>getGenerator(),
'tasks'
=>
$tasks
)
)
);
}
HttpKernel
• Provides a predefined workflow
to convert a Request into a Response
• Events to hook into
• Error Logging (optionally)
Slide 57
Slide 57 text
Let the kernel handle it.
$dispatcher
=
new
EventDispatcher();
$dispatcher-‐>addSubscriber(
new
RouterListener($router-‐>getMatcher())
);
$resolver
=
new
MyControllerResolver($controller);
$kernel
=
new
HttpKernel($dispatcher,
$resolver);
$request
=
Request::createFromGlobals();
$response
=
$kernel-‐>handle($request);
$response-‐>send();
Slide 58
Slide 58 text
Controller Resolver?
interface
ControllerResolverInterface
{
public
function
getController(Request
$request);
public
function
getArguments(Request
$request,
$controller);
}
Slide 59
Slide 59 text
No content
Slide 60
Slide 60 text
No content
Slide 61
Slide 61 text
A look back
$request
=
Request::createFromGlobals();
$response
=
$kernel-‐>handle($request);
Slide 62
Slide 62 text
BrowserKit & CssSelector
Slide 63
Slide 63 text
Base Test
use
Symfony\Component\HttpKernel\Client;
class
FunctionalTestCase
extends
\PHPUnit_Framework_TestCase
{
protected
static
function
createClient()
{
$kernel
=
//
somehow
create
a
kernel
return
new
Client($kernel);
}
}
Slide 64
Slide 64 text
A Test Case
class
IndexPageTest
extends
FunctionalTestCase
{
public
function
testList()
{
$client
=
static::createClient();
$crawler
=
$client-‐>request('GET',
'/');
$this-‐>assertEquals(200,
$client-‐>getResponse()-‐>getStatusCode());
$this-‐>assertCount(1,
$crawler-‐>filter('h1:contains("My
Todos
List")'));
}
}
Loading Configuration Data
$cachePath
=
__DIR__
.
'/../cache/config.php';
$configPath
=
__DIR__
.
'/../config/config.yml';
$configCache
=
new
ConfigCache($cachePath,
true);
if
(!$configCache-‐>isFresh())
{
$resource
=
new
FileResource($configPath);
$code
=
'write($code,
array($resource));
}
$config
=
require
$cachePath;
Slide 72
Slide 72 text
No content
Slide 73
Slide 73 text
Console
Slide 74
Slide 74 text
Console
• Easy setup for CLI scripts
• Output formatting, help system
• Interactive dialogs
Slide 75
Slide 75 text
Commands
use
Symfony\Component\Console\Command\Command;
class
AddTaskCommand
extends
Command
{
public
function
configure()
{
$this-‐>setName('todo:add');
$this-‐>setDescription('Add
a
new
task
to
your
todo
list');
$this-‐>addArgument('title',
InputArgument::OPTIONAL,
'The
task
title');
}
//
...
}
Slide 76
Slide 76 text
The Application
use
Symfony\Component\Console\Application;
$db
=
//
create
PDO
instance
$app
=
new
Application('Todo
List
Helpers');
$app-‐>add(new
AddTaskCommand($db));
$app-‐>add(new
ExpireTasksCommand($db));
$app-‐>run();
Slide 77
Slide 77 text
No content
Slide 78
Slide 78 text
No content
Slide 79
Slide 79 text
Execution
public
function
execute(InputInterface
$input,
OutputInterface
$output)
{
$dialog
=
$this-‐>getHelperSet()-‐>get('dialog');
$title
=
$dialog-‐>ask(
$output,
'What
do
you
have
to
do?
'
);
if
($title)
{
//
do
stuff
$output-‐>writeln('Task
created.');
}
else
{
$output-‐>writeln('No
input
given!');
}
}
Slide 80
Slide 80 text
No content
Slide 81
Slide 81 text
No content
Slide 82
Slide 82 text
What else is there?
• Form
• Security
• Validator
• Event Dispatcher
• Finder
• Process
• PropertyAccess
• OptionsResolver
• ...
Slide 83
Slide 83 text
Go forth and learn!
http://goo.gl/a0bCJ
Slide 84
Slide 84 text
Thanks! Questions?
Please give feedback:
http://goo.gl/IMK9n