Symfony2 For Beginners
Symfony2 For Beginners
Silpion IT-Solutions
Hi, my name is
Julius Beckmann
and i work at
Silpion IT-Solutions
and i do:
PHP, Symfony2
Erlang, Elixir
NodeJS
DevOps + Continuous Everything!
(github|twitter).com/h4cc
Silpion IT-Solutions
Agenda
Silpion IT-Solutions
Agenda 1/2
What is Symfony?
Silpion IT-Solutions
Agenda 1/2
What is Symfony?
Howto
Install
Develop
Deploy
Silpion IT-Solutions
Agenda 2/2
Plain PHP to Symfony
Starting Point
Dispatching
Routing
Symfony HTTP Foundation
Symfony HTTP Kernel
Symfony Routing
Symfony Controller
Silpion IT-Solutions
Agenda 2/2
Plain PHP to Symfony
Starting Point
Dispatching
Routing
Symfony HTTP Foundation
Symfony HTTP Kernel
Symfony Routing
Symfony Controller
Silpion IT-Solutions
What is Symfony?
Silpion IT-Solutions
What is Symfony?
Abstract:
PHP Web Application Framework
At least PHP 5.3.3
By Fabien Potencier (fabpot)
Silpion IT-Solutions
What is Symfony?
Abstract:
PHP Web Application Framework
At least PHP 5.3.3
By Fabien Potencier (fabpot)
Versions:
v1 since 2005 (legacy)
v2 since 2011
v3 in November 2015
Silpion IT-Solutions
Patterns:
Framework Components
Modular design (Bundles)
Dependency Injection
Event Dispatcher
Silpion IT-Solutions
Patterns:
Framework Components
Modular design (Bundles)
Dependency Injection
Event Dispatcher
Used by:
Drupal 8
eZPublish 5
Contao 4
phpBB 3
Silpion IT-Solutions
Installing Symfony
Silpion IT-Solutions
Symfony Installer
A tool for bootstrapping new Symfony projects.
Silpion IT-Solutions
Symfony Installer
A tool for bootstrapping new Symfony projects.
.
Installing the installer
.
curl -LsS http://symfony.com/installer -o symfony.phar
chmod a+x symfony.phar
.
Silpion IT-Solutions
Symfony Installer
A tool for bootstrapping new Symfony projects.
.
Installing the installer
.
curl -LsS http://symfony.com/installer -o symfony.phar
chmod a+x symfony.phar
.
.
Using the installer
.
php symfony.phar new blog
php symfony.phar new blog 2.3
php symfony.phar new blog 2.5.2
.
Silpion IT-Solutions
Composer
Manual way of bootstrapping symfony projects.
php composer.phar create-project \
symfony/framework-standard-edition \
blog "2.3.*"
Silpion IT-Solutions
Composer
Manual way of bootstrapping symfony projects.
php composer.phar create-project \
symfony/framework-standard-edition \
blog "2.3.*"
This will do:
1.
2.
3.
4.
5.
Silpion IT-Solutions
Development
Silpion IT-Solutions
Development
.
Using the PHP >=5.4 internal Webserver
.
php app/console server:run
# or
php -S 127.0.0.1:8080 -t web/
.
Silpion IT-Solutions
Development
.
Using the PHP >=5.4 internal Webserver
.
php app/console server:run
# or
php -S 127.0.0.1:8080 -t web/
.
.
Frontend Tools
.
php app/console assets:install
php app/console assetic:dump
Grunt, Gulp, Brunch
.
Silpion IT-Solutions
Deployment
Silpion IT-Solutions
Deployment
Poor-Man example:
.
Initial
.
git clone [email protected]:your/project.git /var/www
cd /var/www
# ensure correct permissions on app/cache and app/logs
.
Silpion IT-Solutions
Deployment
Poor-Man example:
.
Initial
.
git clone [email protected]:your/project.git /var/www
cd /var/www
# ensure correct permissions on app/cache and app/logs
.
.
Deploy
.
git pull
rm -f web/app_*.php
composer install
php app/console cache:clear --env=prod
# ... more needed commands like "doctrine"
Tools: Capifony, Ansible, SaltStack, Puppet, Chef.
.Automation with: Jenkins.
Symfony2 for Beginners
Silpion IT-Solutions
Silpion IT-Solutions
Starting point
GET /post.php?id=1
<?php // post.php
$postId = (int)$_GET['id'];
$posts = array(1 => 'My first post');
if(!isset($posts[$postId])) {
header("HTTP/1.0 404 Not Found");
die('Post not found');
}
$post = $posts[$postId];
echo '<h1>Post #', $postId, '</h1>
<p>', $post, '</p>';
Symfony2 for Beginners
Silpion IT-Solutions
Webserver Rewrites
Silpion IT-Solutions
Using rewrites
GET /post/1
.
.htaccess Conguration
.
RewriteEngine on
RewriteRule
^post/([0-9]+)$ post.php?id=$1
.
Silpion IT-Solutions
Using rewrites
GET /post/1
.
.htaccess Conguration
.
RewriteEngine on
RewriteRule
^post/([0-9]+)$ post.php?id=$1
.
.
Problems
.
Depends on Webserver
Static conguration
Outside of Application
.
Silpion IT-Solutions
Silpion IT-Solutions
Using dispatcher
.
.htaccess Conguration
.
RewriteEngine On
.RewriteRule ^(.*)$ /index.php [QSA]
Silpion IT-Solutions
Using dispatcher
.
.htaccess Conguration
.
RewriteEngine On
.RewriteRule ^(.*)$ /index.php [QSA]
.
index.php with Routing
.
<?php // index.php
$path = $_SERVER['PATH_INFO'];
// Routing
if(preg_match("@^/post/(?P<id>[0-9]+)@", $path, $m)){
$_GET['id'] = $m['id'];
require('controller/post.php');
}else{
require('controller/index.php');
}
.
Symfony2 for Beginners
Silpion IT-Solutions
Silpion IT-Solutions
1.
Silpion IT-Solutions
1.
2.
Hiding PHP-ServerAPI
$_GET, $_POST, $_SERVER, echo(), header(), die()
Silpion IT-Solutions
1.
2.
Hiding PHP-ServerAPI
$_GET, $_POST, $_SERVER, echo(), header(), die()
3.
Silpion IT-Solutions
Silpion IT-Solutions
<?php
use Symfony\Component\HttpFoundation\Response;
return new Response('Hello World', 200);
Silpion IT-Solutions
<?php
use Symfony\Component\HttpFoundation\Response;
return new Response('Hello World', 200);
Silpion IT-Solutions
Benets
1.
Reproducible Testable
Silpion IT-Solutions
Benets
1.
Reproducible Testable
2.
Silpion IT-Solutions
Silpion IT-Solutions
<?php
interface Symfony\Component\HttpKernel\HttpKernelInterface {
const MASTER_REQUEST = 1; // External request (from browser).
const SUB_REQUEST = 2;
// Internal request.
/** @return Response */
public function handle(Request $request, $type=1, $catch=true);
}
Silpion IT-Solutions
HTTP Kernel
<?php // index.php
class ExampleKernel implements HttpKernelInterface {
public function handle(Request $req) {
// Step 1: Routing
$controller = $this->routeRequestToController($req);
// Step 2: ?
$response = $this->callRequestOnController($controller, $req);
// PROFIT!!!
return $response;
}
// ...
}
$kernel = new ExampleKernel();
$response = $kernel->handle(Request::createFromGlobals());
$response->send();
Symfony2 for Beginners
Silpion IT-Solutions
Symfony Routing
Silpion IT-Solutions
Symfony Routing
.
Goal
.
Find Controller and Action for given Request.
.
Silpion IT-Solutions
Symfony Routing
.
Goal
.
Find Controller and Action for given Request.
.
.
Example Blog Post Route
.
# app/config/routing.yml
blog_post_show:
# Name of route
path: /post/{id}
# Pattern with placeholders
defaults:
_controller: BlogBundle:Post:show
requirements:
id: \d+
# Regex possible
_method: GET
# Ensure used method
.
Symfony2 for Beginners
Silpion IT-Solutions
.
<?php
$router = $this->get('router');
$path = $router->generate('blog_post_show', ['id' => 42]);
$url = $router->generate('blog_post_show', ['id' => 42], true);
.
Silpion IT-Solutions
.
<?php
$router = $this->get('router');
$path = $router->generate('blog_post_show', ['id' => 42]);
$url = $router->generate('blog_post_show', ['id' => 42], true);
.
.
.
<a href="{{ path('blog_post_show', {'id': 42}) }}">
Relative URL
</a>
<a href="{{ url('blog_post_show', {'id': 42}) }}">
Absolute URL
</a>
.
Symfony2 for Beginners
Silpion IT-Solutions
Silpion IT-Solutions
Silpion IT-Solutions
Symfony Controller
Silpion IT-Solutions
Symfony Controller
Silpion IT-Solutions
Symfony Controller
Silpion IT-Solutions
Silpion IT-Solutions
PostController
<?php // src/BlogBundle/Controller/PostController.php
class PostController extends Controller {
public function showAction(Request $request) {
$id = $request->get('id');
$post = $this->getDoctrine()
->getRepository('BlogBundle:Post')
->find($id);
if(!$post) {
// Using exceptions here.
throw $this->createNotFoundException('Post not found');
}
return new Response('<h1>Post #'. $post->getId(). '</h1>
<p>'. $post->getContent(). '</p>', 200);
}
}
Symfony2 for Beginners
Silpion IT-Solutions
Parameter injection
Silpion IT-Solutions
Parameter injection
<?php // src/BlogBundle/Controller/PostController.php
class PostController extends Controller {
public function showAction($id) { // <-- Parameter from route
$post = $this->getDoctrine()
->getRepository('BlogBundle:Post')
->find($id);
if(!$post) {
throw $this->createNotFoundException('Post not found');
}
return new Response('<h1>Post #'. $post->getId(). '</h1>
<p>'. $post->getContent(). '</p>', 200);
}
}
Silpion IT-Solutions
Templating
Silpion IT-Solutions
Templating
<?php // src/BlogBundle/Controller/PostController.php
class PostController extends Controller {
public function showAction($id) {
$post = $this->getDoctrine()
->getRepository('BlogBundle:Post')
->find($id);
if(!$post) {
throw $this->createNotFoundException('Post not found');
}
// Using template system
return $this->render('post/show.html.twig',
}
}
Silpion IT-Solutions
ParamConverter Annotation
Silpion IT-Solutions
ParamConverter Annotation
<?php // src/BlogBundle/Controller/PostController.php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class PostController extends Controller {
/**
* @ParamConverter()
*/
public function showAction(Post $post) { // Post from DB or 404
return $this->render('post/show.html.twig',
}
}
Silpion IT-Solutions
Template Annotation
Silpion IT-Solutions
Template Annotation
<?php // src/BlogBundle/Controller/PostController.php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class PostController extends Controller {
/**
* @ParamConverter()
* @Template("post/show.html.twig")
*/
public function showAction(Post $post) {
return ['post' => $post];
}
}
Silpion IT-Solutions
Template Annotation
<?php // src/BlogBundle/Controller/PostController.php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class PostController extends Controller {
/**
* @ParamConverter()
* @Template("post/show.html.twig", vars={"post"})
*/
public function showAction(Post $post) {}
}
Silpion IT-Solutions
Route Annotation
Silpion IT-Solutions
Route Annotation
<?php // src/BlogBundle/Controller/PostController.php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class PostController extends Controller {
/**
* @Route("/show/{id}", requirements={"id"="\d+", "_method"="GET"})
* @ParamConverter()
* @Template(vars={"post"})
*/
public function showAction(Post $post) {}
}
Silpion IT-Solutions
Silpion IT-Solutions
Kernel Events
Symfony is using Events to process Requests.
Silpion IT-Solutions
Kernel Events
Symfony is using Events to process Requests.
.
Every Request
.
kernel.request
kernel.controller
kernel.response
.
Silpion IT-Solutions
Kernel Events
Symfony is using Events to process Requests.
.
Every Request
.
kernel.request
kernel.controller
kernel.response
.
.
Optional
.
kernel.view
kernel.exception
kernel.terminate
.
Silpion IT-Solutions
Event kernel.request
Silpion IT-Solutions
Resolve Controller
Silpion IT-Solutions
Event kernel.controller
Silpion IT-Solutions
Controller Arguments
Silpion IT-Solutions
Call Controller
Silpion IT-Solutions
Call Controller
Silpion IT-Solutions
Event kernel.view
Silpion IT-Solutions
Event kernel.exception
Silpion IT-Solutions
Silpion IT-Solutions
Questions?
Silpion IT-Solutions
Symfony Sub-Requests
Silpion IT-Solutions
Sub-Requests: Flow
Silpion IT-Solutions
Sub-Requests: Example
.
Forwarding
.
<?php
public function impressumAction($name) {
$response = $this->forward('ExampleContentBundle:Static:imprint');
}
.
Silpion IT-Solutions
Sub-Requests: Example
.
Forwarding
.
<?php
public function impressumAction($name) {
$response = $this->forward('ExampleContentBundle:Static:imprint');
}
.
.
Rendering Templates
.
{{ render(url('/partial/footer.html')) }}
{# ... or ... #}
{{ render(controller('ExampleContentBundle:Partial:footer')) }}
.
Silpion IT-Solutions
The end!
Silpion IT-Solutions