symfony2 for midgard developers

76
Symfony2 for Midgard Developers

Upload: henri-bergius

Post on 10-May-2015

7.791 views

Category:

Technology


1 download

DESCRIPTION

Symfony2 tutorial geared towards Midgard developers

TRANSCRIPT

Page 1: Symfony2 for Midgard Developers

Symfony2 for Midgard Developers

Page 2: Symfony2 for Midgard Developers

Symfony2 is becoming the de-facto web framework for PHP 5.3

Page 3: Symfony2 for Midgard Developers

Drupal

Page 4: Symfony2 for Midgard Developers

Zend Framework

Page 5: Symfony2 for Midgard Developers

Midgard MVC

Page 6: Symfony2 for Midgard Developers

MidCOM

Page 7: Symfony2 for Midgard Developers

The PHP world is starting to unite around Symfony2

Page 8: Symfony2 for Midgard Developers
Page 9: Symfony2 for Midgard Developers
Page 10: Symfony2 for Midgard Developers
Page 11: Symfony2 for Midgard Developers

Under MIT LicenseCopyright (c) 2004-2011 Fabien PotencierPermission 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 followingconditions: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 OROTHER DEALINGS IN THE SOFTWARE.

Page 12: Symfony2 for Midgard Developers

Symfony 2.0 was released on July 28 2011

Page 13: Symfony2 for Midgard Developers
Page 14: Symfony2 for Midgard Developers

Symfony2 is a collection of standalone PHP componentsbundled together into a single framework

Page 15: Symfony2 for Midgard Developers

You can choose what to take, and what to not use

Page 16: Symfony2 for Midgard Developers

✔ Authentication and Authorization✔ Forms and Validation✔ Templating✔ Logging✔ Asset Management✔ Routing✔ Internationalization✔ Console Tasks✔ Caching

Page 17: Symfony2 for Midgard Developers

Dependency Injection means you can changeany of these to another implementation

Page 18: Symfony2 for Midgard Developers

<?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.

Page 19: Symfony2 for Midgard Developers

PHP 5.3 and namespaces

Page 20: Symfony2 for Midgard Developers

Midgard MVC & MidCOM

<?php class example_mycomponent_controllers_some {

Symfony2

<?php namespace Example\MyBundle\Controller; use Symfony\Framework\WebBundle\Controller; class SomeController extends Controller {

Page 21: Symfony2 for Midgard Developers

\ is the new _

Page 22: Symfony2 for Midgard Developers

With namespaces all projects can share one autoloader.

Page 23: Symfony2 for Midgard Developers

<?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();

Page 24: Symfony2 for Midgard Developers

Time to get the hands dirty

Page 25: Symfony2 for Midgard Developers

You'll need:

• PHP 5.3• php5-midgard2• PEAR installer• Git

Page 26: Symfony2 for Midgard Developers

Symfony2 Standard Edition

Page 27: Symfony2 for Midgard Developers

http://symfony.com/download

Page 28: Symfony2 for Midgard Developers
Page 29: 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 ...

Page 30: Symfony2 for Midgard Developers

Dependencies are defined in the deps file

[AsseticBundle] git=http://github.com/symfony/AsseticBundle.git target=/bundles/Symfony/Bundle/AsseticBundle version=v1.0.0RC2

Page 31: Symfony2 for Midgard Developers

Now you could just use the web dirstuff in your regular web server.

Page 32: Symfony2 for Midgard Developers

However, we'll be using AppServer-in-PHP

$ pear channel-discover pear.indeyets.pp.ru $ pear install indeyets/aip

Page 33: Symfony2 for Midgard Developers

Add AiP integration as dependency

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

Install with $ php bin/vendors install --reinstall

Page 34: Symfony2 for Midgard Developers

Copy aip.yaml.example fromvendor/Midgard/AppServerBundleto the app dir as aip.yaml

Page 35: Symfony2 for Midgard Developers

Add Midgard namespace to autoloader

$loader->registerNamespaces(array( ... 'Midgard' => __DIR__.'/../vendor',

Page 36: Symfony2 for Midgard Developers

Start AiP aip app app/aip.yamlGo to http://localhost:8001

Page 37: Symfony2 for Midgard Developers

Now, to write some code!

$ php app/console generate:bundle

Let Symfony2 create a Acme/ExampleBundle for you.

Page 38: Symfony2 for Midgard Developers

src/Acme/ExampleBundle

Page 39: Symfony2 for Midgard Developers

http://localhost:8001/hello/World

Page 40: Symfony2 for Midgard Developers

src/Acme/ExampleBundle/Controller/DefaultController.php

<?php namespace Acme\ExampleBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; class DefaultController extends Controller { /** * @Route("/hello/{name}") * @Template() */ public function indexAction($name) { return array('name' => $name); } }

Page 41: Symfony2 for Midgard Developers

src/Acme/ExampleBundle/Resources/views/Default/index.html.twig

Hello {{ name }}!

Page 42: Symfony2 for Midgard Developers

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 43: Symfony2 for Midgard Developers

Page generation process:

1. Match the URL to a route (/hello/{name}) /hello/World ⇒ /hello/{name}2. Instantiate Controller new Acme\ExampleBundle\Controller\DefaultController3. Run action method with arguments ->indexAction('World')3. Controller returns a Response Hello World!

Page 44: Symfony2 for Midgard Developers

Templating in Symfony2

Page 45: Symfony2 for Midgard Developers

Twig is the default templating engine for Symfony2

Page 46: Symfony2 for Midgard Developers
Page 47: Symfony2 for Midgard Developers
Page 48: 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 thenoverrides.

Page 49: Symfony2 for Midgard Developers

src/Acme/ExampleBundle/Resources/views/Default/index.html.twig

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

{% block content %} Hello {{ name }}! {% endblock %}

Page 50: Symfony2 for Midgard Developers

http://localhost:8001/hello/World

Page 51: Symfony2 for Midgard Developers

You can also use other templating enginesTAL, PHP, MidCOM, ...

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

Page 52: Symfony2 for Midgard Developers

Exercise:

Create your own base layout templateMake the route's template inherit that

Page 53: Symfony2 for Midgard Developers

Routing in Symfony2

Page 54: Symfony2 for Midgard Developers

Registering routes with PHP

app/config/routing.yml

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

In Controller action methods:

/** * @Route("/hello/{name}") */

Page 55: Symfony2 for Midgard Developers

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)

Page 56: Symfony2 for Midgard Developers

$ 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}

Page 57: Symfony2 for Midgard Developers

Supporting other output formats

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

http://localhost:8001/hello/World andhttp://localhost:8001/hello/World.htmlwill both work

Page 58: Symfony2 for Midgard Developers

src/Acme/ExampleBundle/Resources/views/Default/index.json.twig

{% set arr = { 'name': name } %} {{ arr | json_encode | raw }}

Page 59: Symfony2 for Midgard Developers

http://localhost:8001/hello/World.json

Page 60: Symfony2 for Midgard Developers

Exercise:

Create two routes• One with no parameters• One with two parameters• Provide a default value for parameter

Page 61: Symfony2 for Midgard Developers

Using Midgard inside Symfony2

Page 62: Symfony2 for Midgard Developers

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 Midgard\ConnectionBundle\MidgardConnectionBundle() );

Page 63: Symfony2 for Midgard Developers

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"

Page 64: Symfony2 for Midgard Developers

Now you can create a database

$ php app/console midgard:connection:init

Page 65: Symfony2 for Midgard Developers

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); }

Page 66: Symfony2 for Midgard Developers

Hello, Midgard!

Page 67: Symfony2 for Midgard Developers

Exercise:

Create a route displaying all personobjects in the Midgard database

Page 68: Symfony2 for Midgard Developers

MidCOM components inside Symfony2

Page 69: Symfony2 for Midgard Developers

Warning: Here be Dragons

Page 70: Symfony2 for Midgard Developers

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 Midgard\MidcomCompatBundle\MidgardMidcomCompatBundle() );

Page 71: Symfony2 for Midgard Developers

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

Page 72: Symfony2 for Midgard Developers

Enable component in app/AppKernel.php

$bundles = array( ... new Midgard\MidcomCompatBundle\Bundle\ComponentBundle( 'net.nehmer.blog') );

Add a component to routing configuration:

NetNehmerBlog: resource: "net.nehmer.blog" type: midcom prefix: /blog

Page 73: Symfony2 for Midgard Developers

http://localhost:8001/blog/

Page 74: Symfony2 for Midgard Developers

Loading a layout for your MidCOM views

app/config/config.yml

midgard_midcom_compat: layout: "MidgardMidcomCompatBundle::layout.html.twig"

Page 75: Symfony2 for Midgard Developers

Some areas to follow:

• Symfony CMF: node-based routing, etc• PHPCR: standard content repository APIs• MidCOM and Midgard MVC compat work

Page 76: Symfony2 for Midgard Developers

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