PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

10
PHP Frameworks, Libraries & Tools BarCamp Rhein Main 2013 Lukas Rosenstock

Transcript of PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Page 1: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

PHP Frameworks, Libraries & Tools

BarCamp Rhein Main 2013Lukas Rosenstock

Page 2: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Overview

● PHP 5.3: Namespaces, Autoloading and PSR-0● Composer & Packagist Dependency Manager● Silex Microframework & symfony components● Twig Template Engine● Guzzle HTTP Library● Doctrine Object-Relational-Mapper

Page 3: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

PHP 5.3

● Namespace declaration:– namespace LukasRosenstock\Events;

– class BarCamp { }

● Use classes:– $bcrm13 = new LukasRosenstock\Events\BarCamp;

– or– use LukasRosenstock\Events\BarCamp;

– $bcrm13 = new BarCamp;

● PSR0 Guideline: namespace = path– /LukasRosenstock/Events/BarCamp.php

● Autoloading → magic 'require' when needed

Page 4: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Composer & Packagist

● Per-project dependency management for frameworks, libraries etc.– Dependencies are not included in project repository

● Command-line tool composer.phar– composer install

● JSON configuration files composer.json for each package– Public package names → http://packagist.org/

– Custom repositories and downloads

● http://getcomposer.org/

Page 5: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Silex Microframework

● Microframework = single PHP file for multiple URL patterns– Good for RESTful APIs

– Requires URL rewriting in webserver

● Implementation with closures– $app = new Silex\Application;

– $app->get('/event/{id}', function($id) { return 'You have requested event '.$id; });

– $app->post('/event/{id}', function($id, Request $r) { return 'Event updated with '.$r->getContent(); });

– $app->run();

● http://silex.sensiolabs.org/

Page 6: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Twig Template Engine

● Separation logic and view● Frontend developers should not write PHP

– Twig has own syntax for conditions, loops etc.

● Template:– <h1>Event {{name}}</h1>

– {% if is_today %}<p>Happening today.</p>{% endif %}

● PHP Code:– $twig = new \Twig_Environment(new

\Twig_Loader_Filesystem('/views'));

– Return $twig->render('event.twig.html', array('name' => $event->getName(), 'is_today' => $event->getDate()==$now);

● http://twig.sensiolabs.org/

Page 7: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Guzzle HTTP Library

● OOP abstraction layer for libcurl● Extendable with plugins● Service definitions for webservice API clients

– Amazon WebServices PHP SDK

● Example:– $client = new

Guzzle\Http\Client('http://api.example.com/');

– $client->setDefaultHeaders(array('Authorization' => 'Bearer '.$accessToken));

– $response = $client->get('/event/bcrm13')->send();

– $eventData = $response->json();

● http://guzzlephp.org/

Page 8: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Doctrine Object-Relational-Mapper

● 1 SQL table = 1 PHP class– Mapping of fields and relations defined with

annotations (comments) or in external configuration files

● Sample fields:– /** @Id @Column(type="integer") **/ private $id;

– /** @Column(type="string") **/ private $name;

– /** @OneToOne(targetEntity="User") **/ private $organizer;

● Creating Entity:– $event = new LukasRosenstock\Events\BarCamp;

– $em->persist($event); // em = EntityManager

– $em->flush();

Page 9: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Doctrine Object-Relational-Mapper

● Fetching Entity:– $event = $em-

>getRepository('LukasRosenstock\Events\BarCamp')->findOneById(1);

● Modifying Entity:– $user = new LukasRosenstock\Events\Organizer;

– $event->setOrganizer($user);

– $em->flush();

● http://www.doctrine-project.org/

Page 10: PHP Frameworks, Libraries & Tools - BarCamp RheinMain 2013

Thank you!

Questions?!