SlideShare a Scribd company logo
Symfony2 for Midgard Developers
Symfony2 is becoming the
de-facto web framework for PHP 5.3
Drupal
Zend Framework
Midgard MVC
MidCOM
The PHP world is starting to unite around Symfony2
Symfony2 for Midgard Developers
Symfony2 for Midgard Developers
Symfony2 for Midgard Developers
Under MIT License
Copyright (c) 2004-2011 Fabien Potencier
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Symfony 2.0 was released on July 28 2011
Symfony2 for Midgard Developers
Symfony2 is a collection of standalone PHP components
bundled together into a single framework
You can choose what to take, and what to not use
✔   Authentication and Authorization
✔   Forms and Validation
✔   Templating
✔   Logging
✔   Asset Management
✔   Routing
✔   Internationalization
✔   Console Tasks
✔   Caching
Dependency Injection means you can change
any of these to another implementation
<?php
 $this->container->get('security.context')->isGranted(
  'READ', $object)

The actual service checking this could be
MidCOM-style ACL, or simple "allow authenticated
users to do anything" check.
PHP 5.3 and namespaces
Midgard MVC & MidCOM

<?php
class example_mycomponent_controllers_some
{

Symfony2

<?php
namespace ExampleMyBundleController;
use SymfonyFrameworkWebBundleController;
class SomeController extends Controller
{
is the new _
With namespaces all projects can share one autoloader.
<?php
$crRoot = realpath(__DIR__ . '/..');

// register the autoloaders
require "{$crRoot}/SplClassLoader.php";

// Tell where Midgard stuff is
$midgardAutoloader = new SplClassLoader('Midgard', "{$crRoot}/src");
$midgardAutoloader->register();

// Tell where code from some other project is
$phpcrAutoloader = new SplClassLoader('PHPCR', "{$crRoot}/lib/PHPCR/src");
$phpcrAutoloader->register();
Time to get the hands dirty
You'll need:

• PHP 5.3
• php5-midgard2
• PEAR installer
• Git
Symfony2 Standard Edition
http://symfony.com/download
Symfony2 for Midgard Developers
$ php app/check.php
********************************
*                              *
* Symfony requirements check *
*                              *
********************************

php.ini used by PHP: /etc/php5/cli/php.ini

** Mandatory requirements **

OK        Checking that PHP version is at least 5.3.2 (5.3.5-1ubuntu7.2 installed)
OK        Checking that the "date.timezone" setting is set
OK        Checking that app/cache/ directory is writable
...
Dependencies are defined in the deps file

[AsseticBundle]
  git=http://github.com/symfony/AsseticBundle.git
  target=/bundles/Symfony/Bundle/AsseticBundle
  version=v1.0.0RC2
Now you could just use the web dir
stuff in your regular web server.
However, we'll be using AppServer-in-PHP

$ pear channel-discover pear.indeyets.pp.ru
$ pear install indeyets/aip
Add AiP integration as dependency

[AppServerBundle]
  git=http://github.com/bergie/MidgardAppServerBundle.git
  target=Midgard/AppServerBundle

Install with $ php bin/vendors install --reinstall
Copy aip.yaml.example from
vendor/Midgard/AppServerBundle
to the app dir as aip.yaml
Add Midgard namespace to autoloader

$loader->registerNamespaces(array(
  ...
  'Midgard'      => __DIR__.'/../vendor',
Start AiP aip app app/aip.yaml
Go to http://localhost:8001
Now, to write some code!

$ php app/console generate:bundle

Let Symfony2 create a Acme/ExampleBundle for you.
src/Acme/ExampleBundle
http://localhost:8001/hello/World
src/Acme/ExampleBundle/Controller/DefaultController.php

<?php
namespace AcmeExampleBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationTemplate;
class DefaultController extends Controller
{
   /**
    * @Route("/hello/{name}")
    * @Template()
    */
   public function indexAction($name)
   {
      return array('name' => $name);
   }
}
src/Acme/ExampleBundle/Resources/views/Default/index.html.twig

Hello {{ name }}!
How did this template get loaded?

/**
 * @Template()
 */
...
return array('name' => $name);

⇒ Defaults to Bundle/Resources/views/Controller/action.html.twig

We could also do:

return $this->render(
   'AcmeExampleBundle:Default:index.html.twig',
   array('name' => $name)
);
Page generation process:

1. Match the URL to a route (/hello/{name})
   /hello/World ⇒ /hello/{name}
2. Instantiate Controller
   new AcmeExampleBundleControllerDefaultController
3. Run action method with arguments
   ->indexAction('World')
3. Controller returns a Response
   Hello World!
Templating in Symfony2
Twig is the default templating engine for Symfony2
Symfony2 for Midgard Developers
Symfony2 for Midgard Developers
In Midgard you only define the elements you want to override

In Twig you define a new root element that inherits and then
overrides.
src/Acme/ExampleBundle/Resources/views/Default/index.html.twig

{% extends 'AcmeDemoBundle::layout.html.twig' %}

{% block content %}
Hello {{ name }}!
{% endblock %}
http://localhost:8001/hello/World
You can also use other templating engines
TAL, PHP, MidCOM, ...

http://symfony.com/doc/2.0/cookbook/templating/PHP.html
Exercise:

Create your own base layout template
Make the route's template inherit that
Routing in Symfony2
Registering routes with PHP

app/config/routing.yml

AcmeExampleBundle:
  resource: "@AcmeExampleBundle/Controller/"
  type: annotation
  prefix: /

In Controller action methods:

/**
 * @Route("/hello/{name}")
 */
Registering routes with YAML

AcmeExampleBundle:
  resource: "@AcmeExampleBundle/Resources/config/routing.yml"
  prefix: /example

src/Acme/ExampleBundle/Resources/config/routing.yml

hello_user:
  pattern: /hello/{name}
  defaults: { _controller: AcmeExampleBundle:Default:index}

(format is NamespaceBundle:Controller:action)
$ php app/console router:debug

[router] Current routes
Name                         Method   Pattern
_welcome                     ANY      /
_demo_login                  ANY      /demo/secured/login
_security_check              ANY      /demo/secured/login_check
_demo_logout                 ANY      /demo/secured/logout
...
_configurator_final          ANY      /_configurator/final
acme_example_default_index   ANY      /example/hello/{name}
Supporting other output formats

/**
 * @Route("/hello/{name}.{_format}", defaults={"_format"="html"})
 */

http://localhost:8001/hello/World and
http://localhost:8001/hello/World.html
will both work
src/Acme/ExampleBundle/Resources/views/Default/index.json.twig

{% set arr = { 'name': name } %}
{{ arr | json_encode | raw }}
http://localhost:8001/hello/World.json
Exercise:

Create two routes
• One with no parameters
• One with two parameters
• Provide a default value for parameter
Using Midgard inside Symfony2
Add Midgard ConnectionBundle as dependency

[MidgardConnectionBundle]
  git=git://github.com/bergie/MidgardConnectionBundle.git
  target=Midgard/ConnectionBundle

Install with $ php bin/vendors install

Enable in app/AppKernel.php

$bundles = array(
   ...
   new MidgardConnectionBundleMidgardConnectionBundle()
);
Configure Midgard connection

app/config/config.yml

midgard_connection:
  type: SQLite
  name: midgard2
  databasedir: "%kernel.root_dir%"
  logfile: "%kernel.root_dir%/logs/midgard2.log"
  loglevel: debug
  blobdir: "%kernel.root_dir%/blobs"
  sharedir: "/usr/share/midgard2"
Now you can create a database

$ php app/console midgard:connection:init
Using Midgard in controllers

src/Acme/ExampleBundle/Controller/DefaultController.php

/**
 * @Route("/hello/{name}.{_format}", defaults={"_format"="html"})
 * @Template()
 */
public function indexAction($name)
{
   $qb = new midgard_query_builder('midgard_person');
   $qb->add_constraint('firstname', '=', 'Midgard');
   $persons = $qb->execute();
   return array('name' => $persons[0]->firstname);
}
Hello, Midgard!
Exercise:

Create a route displaying all person
objects in the Midgard database
MidCOM components inside Symfony2
Warning: Here be Dragons
Add Midgard MidcomCompatBundle as dependency

[MidcomCompatBundle]
  git=git://github.com/bergie/MidgardMidcomCompatBundle.git
  target=Midgard/MidcomCompatBundle

Install with $ php bin/vendors install

Enable in app/AppKernel.php

$bundles = array(
   ...
   new MidgardMidcomCompatBundleMidgardMidcomCompatBundle()
);
Install Flack's version of MidCOM

$ git clone https://github.com/flack/openpsa.git

Copy schemas from openpsa/schemas to MgdSchema dir

app/config/config.yml

midgard_midcom_compat:
  root: "%kernel.root_dir%/../openpsa/lib"

framework:
   templating: { engines: ['twig', 'midcom'] }

Run $ php app/console midgard:connection:init
Enable component in app/AppKernel.php

$bundles = array(
   ...
   new MidgardMidcomCompatBundleBundleComponentBundle(
      'net.nehmer.blog')
);

Add a component to routing configuration:

NetNehmerBlog:
  resource: "net.nehmer.blog"
  type: midcom
  prefix: /blog
http://localhost:8001/blog/
Loading a layout for your MidCOM views

app/config/config.yml

midgard_midcom_compat:
 layout: "MidgardMidcomCompatBundle::layout.html.twig"
Some areas to follow:

• Symfony CMF: node-based routing, etc
• PHPCR: standard content repository APIs
• MidCOM and Midgard MVC compat work
Photos:
http://www.flickr.com/photos/bfs_man/4939624151/
http://www.flickr.com/photos/jeremyhiebert/5105804864/
http://www.flickr.com/photos/donkeyhotey/5527263186/
http://www.flickr.com/photos/eustaquio/2750429774/
http://www.flickr.com/photos/hatwar/5022365448/
http://www.flickr.com/photos/jordanfischer/72510316/
http://www.flickr.com/photos/bellatrix6/130907944/

Inspiration:
http://www.slideshare.net/weaverryan/handson-with-the-symfony2-framework

More Related Content

PDF
Midgard Create and VIE
PDF
VIE - Using RDFa to make content editable
PDF
Create JS - A new kind of web editing interface
PDF
Refactoring to a Single Page Application
PPTX
Single Page WebApp Architecture
PPTX
Single page application and Framework
PPTX
Building single page applications
PDF
Modern Web App Architectures
Midgard Create and VIE
VIE - Using RDFa to make content editable
Create JS - A new kind of web editing interface
Refactoring to a Single Page Application
Single Page WebApp Architecture
Single page application and Framework
Building single page applications
Modern Web App Architectures

What's hot (20)

PPTX
Codegen2021 blazor mobile
PDF
Keystone.js 101
PDF
Blazor - The New Silverlight?
PPT
JAVA SCRIPT
PPTX
Single Page Applications on JavaScript and ASP.NET MVC4
PDF
Blazor web apps
PPTX
PPTX
A Gentle Introduction to Blazor
PPTX
Angular on ASP.NET MVC 6
PPTX
DevPira2019 - Blazor
PDF
Ajax difference faqs compiled- 1
PPTX
Developing JavaScript Widgets
DOCX
Client side vs server side
PDF
Single Page Application Best practices
PDF
The MEAN stack - SoCalCodeCamp - june 29th 2014
PPTX
Java script an introduction
PDF
Beginning MEAN Stack
KEY
An Introduction to webOS
PPT
You Know WebOS
PPTX
Building Web Apps with WebAssembly and Blazor
Codegen2021 blazor mobile
Keystone.js 101
Blazor - The New Silverlight?
JAVA SCRIPT
Single Page Applications on JavaScript and ASP.NET MVC4
Blazor web apps
A Gentle Introduction to Blazor
Angular on ASP.NET MVC 6
DevPira2019 - Blazor
Ajax difference faqs compiled- 1
Developing JavaScript Widgets
Client side vs server side
Single Page Application Best practices
The MEAN stack - SoCalCodeCamp - june 29th 2014
Java script an introduction
Beginning MEAN Stack
An Introduction to webOS
You Know WebOS
Building Web Apps with WebAssembly and Blazor
Ad

Similar to Symfony2 for Midgard Developers (20)

ODP
Capifony. Minsk PHP MeetUp #11
PDF
Creating a modern web application using Symfony API Platform, ReactJS and Red...
PDF
Deprecated: Foundations of Zend Framework 2
PPTX
Creating your own framework on top of Symfony2 Components
PPT
Red5 - PHUG Workshops
PDF
Magento 2 Development
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
PDF
Symfony2 revealed
PDF
Desymfony 2011 - Habemus Bundles
PDF
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...
PDF
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
KEY
Improving QA on PHP projects - confoo 2011
KEY
Php Power Tools
PDF
Porting Rails Apps to High Availability Systems
ODP
Pyramid deployment
KEY
Composer
PDF
Deploying Symfony | symfony.cat
PPTX
Rails Engine | Modular application
PPTX
Cloud Community Engineering - Holiday readiness
PDF
Pyramid Deployment and Maintenance
Capifony. Minsk PHP MeetUp #11
Creating a modern web application using Symfony API Platform, ReactJS and Red...
Deprecated: Foundations of Zend Framework 2
Creating your own framework on top of Symfony2 Components
Red5 - PHUG Workshops
Magento 2 Development
Zend Framework 1.9 Setup & Using Zend_Tool
Symfony2 revealed
Desymfony 2011 - Habemus Bundles
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Improving QA on PHP projects - confoo 2011
Php Power Tools
Porting Rails Apps to High Availability Systems
Pyramid deployment
Composer
Deploying Symfony | symfony.cat
Rails Engine | Modular application
Cloud Community Engineering - Holiday readiness
Pyramid Deployment and Maintenance
Ad

More from Henri Bergius (20)

PDF
Decoupling Content Management with Create.js
PDF
Decoupling Content Management
PDF
Create.js - Inline editing for any website
PDF
Decoupling Content Management with Create.js and PHPCR
PDF
Bisnesdata - Tietojärjestelmien kätköistä tableteille
PDF
Create - Decoupled CMS interface
PDF
Proggis - Business Analytics with Linked Data
ODP
CreateJS hackathon in Zurich
PDF
NoFlo - Flow-Based Programming for Node.js
PDF
PHPCR - Standard Content Repository for PHP
PDF
Location awareness in MeeGo
PDF
Midgard Create and editing content via RDFa
PDF
Semantic editor
PDF
Midgard2 - Content Repository for mobile applications
PDF
Midgard2 Content Repository at FSCONS 2009
ODP
Location-aware applications with GeoClue
PDF
Midgard & Nemein - when an open source project and company evolve together
PDF
ODP
Location-aware desktop
PDF
Midgard2: Content repository for desktop and the web
Decoupling Content Management with Create.js
Decoupling Content Management
Create.js - Inline editing for any website
Decoupling Content Management with Create.js and PHPCR
Bisnesdata - Tietojärjestelmien kätköistä tableteille
Create - Decoupled CMS interface
Proggis - Business Analytics with Linked Data
CreateJS hackathon in Zurich
NoFlo - Flow-Based Programming for Node.js
PHPCR - Standard Content Repository for PHP
Location awareness in MeeGo
Midgard Create and editing content via RDFa
Semantic editor
Midgard2 - Content Repository for mobile applications
Midgard2 Content Repository at FSCONS 2009
Location-aware applications with GeoClue
Midgard & Nemein - when an open source project and company evolve together
Location-aware desktop
Midgard2: Content repository for desktop and the web

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
sap open course for s4hana steps from ECC to s4
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton

Symfony2 for Midgard Developers