Your SlideShare is downloading. ×
  • Like
Symfony2 for Midgard Developers
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Symfony2 for Midgard Developers


Symfony2 tutorial geared towards Midgard developers

Symfony2 tutorial geared towards Midgard developers

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Symfony2 for Midgard Developers
  • 2. Symfony2 is becoming thede-facto web framework for PHP 5.3
  • 3. Drupal
  • 4. Zend Framework
  • 5. Midgard MVC
  • 6. MidCOM
  • 7. The PHP world is starting to unite around Symfony2
  • 8. Under MIT LicenseCopyright (c) 2004-2011 Fabien PotencierPermission is hereby granted, free of charge, to any personobtaining a copy of this software and associated documentationfiles (the "Software"), to deal in the Software withoutrestriction, including without limitation the rights to use,copy, modify, merge, publish, distribute, sublicense, and/orsell copies of the Software, and to permit persons to whomthe Software is furnished to do so, subject to the followingconditions:The above copyright notice and this permission notice shallbe included in all copies or substantial portions of theSoftware.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANYKIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THEWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSEAND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHTHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISINGFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.
  • 9. Symfony 2.0 was released on July 28 2011
  • 10. Symfony2 is a collection of standalone PHP componentsbundled together into a single framework
  • 11. You can choose what to take, and what to not use
  • 12. ✔ Authentication and Authorization✔ Forms and Validation✔ Templating✔ Logging✔ Asset Management✔ Routing✔ Internationalization✔ Console Tasks✔ Caching
  • 13. Dependency Injection means you can changeany of these to another implementation
  • 14. <?php $this->container->get(security.context)->isGranted( READ, $object)The actual service checking this could beMidCOM-style ACL, or simple "allow authenticatedusers to do anything" check.
  • 15. PHP 5.3 and namespaces
  • 16. Midgard MVC & MidCOM<?phpclass example_mycomponent_controllers_some{Symfony2<?phpnamespace ExampleMyBundleController;use SymfonyFrameworkWebBundleController;class SomeController extends Controller{
  • 17. is the new _
  • 18. With namespaces all projects can share one autoloader.
  • 19. <?php$crRoot = realpath(__DIR__ . /..);// register the autoloadersrequire "{$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();
  • 20. Time to get the hands dirty
  • 21. Youll need:• PHP 5.3• php5-midgard2• PEAR installer• Git
  • 22. Symfony2 Standard Edition
  • 23.
  • 24. $ 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 setOK Checking that app/cache/ directory is writable...
  • 25. Dependencies are defined in the deps file[AsseticBundle] git= target=/bundles/Symfony/Bundle/AsseticBundle version=v1.0.0RC2
  • 26. Now you could just use the web dirstuff in your regular web server.
  • 27. However, well be using AppServer-in-PHP$ pear channel-discover$ pear install indeyets/aip
  • 28. Add AiP integration as dependency[AppServerBundle] git= target=Midgard/AppServerBundleInstall with $ php bin/vendors install --reinstall
  • 29. Copy aip.yaml.example fromvendor/Midgard/AppServerBundleto the app dir as aip.yaml
  • 30. Add Midgard namespace to autoloader$loader->registerNamespaces(array( ... Midgard => __DIR__./../vendor,
  • 31. Start AiP aip app app/aip.yamlGo to http://localhost:8001
  • 32. Now, to write some code!$ php app/console generate:bundleLet Symfony2 create a Acme/ExampleBundle for you.
  • 33. src/Acme/ExampleBundle
  • 34. http://localhost:8001/hello/World
  • 35. src/Acme/ExampleBundle/Controller/DefaultController.php<?phpnamespace AcmeExampleBundleController;use SymfonyBundleFrameworkBundleControllerController;use SensioBundleFrameworkExtraBundleConfigurationRoute;use SensioBundleFrameworkExtraBundleConfigurationTemplate;class DefaultController extends Controller{ /** * @Route("/hello/{name}") * @Template() */ public function indexAction($name) { return array(name => $name); }}
  • 36. src/Acme/ExampleBundle/Resources/views/Default/index.html.twigHello {{ name }}!
  • 37. How did this template get loaded?/** * @Template() */...return array(name => $name);⇒ Defaults to Bundle/Resources/views/Controller/action.html.twigWe could also do:return $this->render( AcmeExampleBundle:Default:index.html.twig, array(name => $name));
  • 38. Page generation process:1. Match the URL to a route (/hello/{name}) /hello/World ⇒ /hello/{name}2. Instantiate Controller new AcmeExampleBundleControllerDefaultController3. Run action method with arguments ->indexAction(World)3. Controller returns a Response Hello World!
  • 39. Templating in Symfony2
  • 40. Twig is the default templating engine for Symfony2
  • 41. In Midgard you only define the elements you want to overrideIn Twig you define a new root element that inherits and thenoverrides.
  • 42. src/Acme/ExampleBundle/Resources/views/Default/index.html.twig{% extends AcmeDemoBundle::layout.html.twig %}{% block content %}Hello {{ name }}!{% endblock %}
  • 43. http://localhost:8001/hello/World
  • 44. You can also use other templating enginesTAL, PHP, MidCOM, ...
  • 45. Exercise:Create your own base layout templateMake the routes template inherit that
  • 46. Routing in Symfony2
  • 47. Registering routes with PHPapp/config/routing.ymlAcmeExampleBundle: resource: "@AcmeExampleBundle/Controller/" type: annotation prefix: /In Controller action methods:/** * @Route("/hello/{name}") */
  • 48. Registering routes with YAMLAcmeExampleBundle: resource: "@AcmeExampleBundle/Resources/config/routing.yml" prefix: /examplesrc/Acme/ExampleBundle/Resources/config/routing.ymlhello_user: pattern: /hello/{name} defaults: { _controller: AcmeExampleBundle:Default:index}(format is NamespaceBundle:Controller:action)
  • 49. $ php app/console router:debug[router] Current routesName 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/finalacme_example_default_index ANY /example/hello/{name}
  • 50. Supporting other output formats/** * @Route("/hello/{name}.{_format}", defaults={"_format"="html"}) */http://localhost:8001/hello/World andhttp://localhost:8001/hello/World.htmlwill both work
  • 51. src/Acme/ExampleBundle/Resources/views/Default/index.json.twig{% set arr = { name: name } %}{{ arr | json_encode | raw }}
  • 52. http://localhost:8001/hello/World.json
  • 53. Exercise:Create two routes• One with no parameters• One with two parameters• Provide a default value for parameter
  • 54. Using Midgard inside Symfony2
  • 55. Add Midgard ConnectionBundle as dependency[MidgardConnectionBundle] git=git:// target=Midgard/ConnectionBundleInstall with $ php bin/vendors installEnable in app/AppKernel.php$bundles = array( ... new MidgardConnectionBundleMidgardConnectionBundle());
  • 56. Configure Midgard connectionapp/config/config.ymlmidgard_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"
  • 57. Now you can create a database$ php app/console midgard:connection:init
  • 58. Using Midgard in controllerssrc/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);}
  • 59. Hello, Midgard!
  • 60. Exercise:Create a route displaying all personobjects in the Midgard database
  • 61. MidCOM components inside Symfony2
  • 62. Warning: Here be Dragons
  • 63. Add Midgard MidcomCompatBundle as dependency[MidcomCompatBundle] git=git:// target=Midgard/MidcomCompatBundleInstall with $ php bin/vendors installEnable in app/AppKernel.php$bundles = array( ... new MidgardMidcomCompatBundleMidgardMidcomCompatBundle());
  • 64. Install Flacks version of MidCOM$ git clone schemas from openpsa/schemas to MgdSchema dirapp/config/config.ymlmidgard_midcom_compat: root: "%kernel.root_dir%/../openpsa/lib"framework: templating: { engines: [twig, midcom] }Run $ php app/console midgard:connection:init
  • 65. Enable component in app/AppKernel.php$bundles = array( ... new MidgardMidcomCompatBundleBundleComponentBundle(;Add a component to routing configuration:NetNehmerBlog: resource: "" type: midcom prefix: /blog
  • 66. http://localhost:8001/blog/
  • 67. Loading a layout for your MidCOM viewsapp/config/config.ymlmidgard_midcom_compat: layout: "MidgardMidcomCompatBundle::layout.html.twig"
  • 68. Some areas to follow:• Symfony CMF: node-based routing, etc• PHPCR: standard content repository APIs• MidCOM and Midgard MVC compat work
  • 69. Photos: