drehbuch zum talk "rapid prototyping mit php frameworks"

14

Click here to load reader

Upload: ralf-eggert

Post on 10-May-2015

4.224 views

Category:

Technology


0 download

DESCRIPTION

Das Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks" auf der Web-Developer-Conference kompakt 2013 zeigt die schrittweisen Aufbau eines Prototypen anhand des ZF2 Folien unter http://de.slideshare.net/eggertralf/rapidprototypingzf2 zu finden

TRANSCRIPT

Page 1: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

1. Projekt einrichten

a) SkeletonApplication installieren$ cd /home/devhost/$ git clone https://github.com/zendframework/ZendSkeletonApplication wdc-zf2 $ cd wdc-zf2/$ ls -al$ php composer.phar selfupdate$ php composer.phar install

b) ZFTool installieren$ php composer.phar require zendframework/zftool:dev-master $ ./vendor/bin/zf.php$ ./vendor/bin/zf.php modules$ ./vendor/bin/zf.php config list

c) Virtual Host einrichten$ sudo nano /etc/apache2/sites-available/wdc-zf2.conf

<VirtualHost 127.0.0.1> ServerName wdc-zf2 DocumentRoot /home/devhost/wdc-zf2/public/ AccessFileName .htaccess SetEnv APPLICATION_ENV development <Directory "/home/devhost/wdc-zf2/public/"> Options All AllowOverride All Require all granted </Directory> </VirtualHost>

$ sudo a2ensite wdc-zf2.conf$ sudo nano /etc/hosts

[...]127.0.0.1 wdc-zf2 [...]

$ sudo service apache2 restart

✔ Im Browser öffnen: http://wdc-zf2/

d) SQLite Datenbank einrichten$ mkdir data/db $ sqlite3 data/db/wdc-zf2.db

e) PhpStorm Projekt einrichten (oder andere IDE der Wahl)

f) ZendDeveloperTools installieren$ php composer.phar require zendframework/zend-developer-tools:dev-master$ cp vendor/zendframework/zend-developer-tools/config/zenddevelopertools.local.php.dist config/autoload/zdt.local.php

✔ In PhpStorm bearbeiten: /config/application.config.php

'modules' => array( 'Application', 'ZendDeveloperTools', ),

✔ Im Browser öffnen: http://wdc-zf2/

Web Developer Conference 2013 Seite 1 von 14

Page 2: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

g) Doctrine 2 installieren$ php composer.phar require doctrine/doctrine-orm-module

✔ In PhpStorm bearbeiten: /config/application.config.php

'modules' => array( [...] 'DoctrineModule', 'DoctrineORMModule', ),

$ mkdir data/DoctrineORMModule$ mkdir data/DoctrineORMModule/Proxy$ chmod -R 777 data

✔ In PhpStorm erstellen: /config/autoload/database.local.php

<?phpreturn array( 'doctrine' => array( 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver', 'params' => array( 'path' => __DIR__ . '/../../data/db/wdc-zf2.db', ), ), ), ),);

✔ Im Browser öffnen: http://wdc-zf2/✔ ZendDeveloperToolbar checken

Web Developer Conference 2013 Seite 2 von 14

Page 3: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

2. Gallery Modul einrichten

a) ZF2 Modul anlegen$ php vendor/bin/zf.php create module Gallery $ php vendor/bin/zf.php create controller gallery Gallery$ php vendor/bin/zf.php create action index gallery Gallery$ php vendor/bin/zf.php create action create gallery Gallery $ php vendor/bin/zf.php create action update gallery Gallery $ php vendor/bin/zf.php create action delete gallery Gallery $ php vendor/bin/zf.php create action show gallery Gallery $ mkdir public/img/gallery$ chmod 777 public/img/gallery/

✔ In PhpStorm bearbeiten: /module/Gallery/config/module.config.php

<?phpreturn array( 'router' => array( 'routes' => array( 'gallery' => array( 'type' => 'Literal', 'options' => array( 'route' => '/gallery', 'defaults' => array( 'controller' => 'Gallery\Controller\Gallery', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'action' => array( 'type' => 'Segment', 'options' => array( 'route' => '/:action[/:id]', 'constraints' => array( 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[0-9]*', ), 'defaults' => array( ), ), ), ), ), ), ), 'controllers' => array( 'invokables' => array( 'Gallery\Controller\Gallery' => 'Gallery\Controller\GalleryController', ), ), 'view_manager' => array( 'template_path_stack' => array( __DIR__ . '/../view', ), ),);

✔ Im Browser öffnen: http://wdc-zf2/gallery/

Web Developer Conference 2013 Seite 3 von 14

Page 4: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

b) Gallery Entity anlegen

✔ In PhpStorm erstellen: /module/Gallery/src/Gallery/Entity/Gallery.php

<?phpnamespace Gallery\Entity;

use Doctrine\ORM\Mapping as ORM;

/** * @ORM\Entity * @ORM\Table(name="gallery") */class Gallery{ /** * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * @ORM\Column(type="integer") */ protected $id;

/** @ORM\Column(type="string") */ protected $title;

/** @ORM\Column(type="string") */ protected $thumburl;

/** @ORM\Column(type="string") */ protected $bigurl;

public function getId() { return $this->id; } public function setId($id) { $this->id = $id; }

public function getTitle() { return $this->title; } public function setTitle($title) { $this->title = $title; }

public function getThumburl() { return $this->thumburl; } public function setThumburl($thumburl) { $this->thumburl = $thumburl; }

public function getBigurl() { return $this->bigurl; } public function setBigurl($bigurl) { $this->bigurl = $bigurl; }}

Web Developer Conference 2013 Seite 4 von 14

Page 5: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

✔ In PhpStorm bearbeiten: /module/Gallery/config/module.config.php

<?phpreturn array( [...]

'doctrine' => array( 'driver' => array( 'gallery_entities' => array( 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 'cache' => 'array', 'paths' => array(__DIR__ . '/../src/Gallery/Entity') ),

'orm_default' => array( 'drivers' => array( 'Gallery\Entity' => 'gallery_entities' ) ) ) ),);

✔ Im Browser öffnen: http://wdc-zf2/gallery/ ✔ ZendDeveloperToolbar checken

$ ./vendor/bin/doctrine-module orm:validate-schema$ ./vendor/bin/doctrine-module orm:schema-tool:create

✔ Im Browser öffnen: http://devhost/phpliteadmin/

c) Testdatensatz anlegen

✔ In PhpStorm bearbeiten: /module/Gallery/src/Gallery/Controller/GalleryController.php

<?phpnamespace Gallery\Controller;

use Gallery\Entity\Gallery;use Zend\Math\Rand;use Zend\Mvc\Controller\AbstractActionController;use Zend\View\Model\ViewModel;

class GalleryController extends AbstractActionController{ public function createAction() { $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('Doctrine\ORM\EntityManager');

$randomKey = Rand::getInteger(10000, 99999);

$gallery = new Gallery(); $gallery->setTitle('Testbild ' . $randomKey); $gallery->setThumburl('thumb-' . $randomKey . '.png'); $gallery->setBigurl('image-' . $randomKey . '.png');

$objectManager->persist($gallery); $objectManager->flush();

return new ViewModel(array( 'gallery' => $gallery, )); }}

Web Developer Conference 2013 Seite 5 von 14

Page 6: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/create.phtml

<?php \Zend\Debug\Debug::dump($this->gallery); ?><hr><a href="<?php echo $this->url('gallery') ?>">Gallery</a>

✔ Im Browser öffnen: http://wdc-zf2/gallery/create/

Web Developer Conference 2013 Seite 6 von 14

Page 7: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

3. Gallery Modul implementieren

a) Alle Datensätze ausgeben

✔ In PhpStorm bearbeiten: /module/Gallery/src/Gallery/Controller/GalleryController.php

namespace Gallery\Controller;

[...]

class GalleryController extends AbstractActionController{ public function indexAction() { $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('Doctrine\ORM\EntityManager'); $repository = $objectManager->getRepository('Gallery\Entity\Gallery');

$galleryList = $repository->findAll();

return new ViewModel( array( 'galleryList' => $galleryList, ) ); } [...]}

✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/index.phtml

<?phpuse Gallery\Entity\Gallery;

$create = array('action' => 'create');?><h1>Gallery</h1><table class="table"> <thead> <tr> <th>ID</th> <th>Title</th> <th>Thumburl</th> <th>Bigurl</th> <th><a href="<?php echo $this->url('gallery/action', $create) ?>"> anlegen </a></th> </tr> </thead> <tbody> <?php foreach ($this->galleryList as $gallery) : /* @var $gallery Gallery */ ?> <?php $show = array('action' => 'show', 'id' => $gallery->getId()); ?> <tr> <td><?php echo $gallery->getId(); ?></td> <td><?php echo $gallery->getTitle(); ?></td> <td><?php echo $gallery->getThumburl(); ?></td> <td><?php echo $gallery->getBigurl(); ?></td> <td><a href="<?php echo $this->url('gallery/action', $show) ?>"> anzeigen </a></td> </tr> <?php endforeach; ?> </tbody></table>

✔ Im Browser öffnen: http://wdc-zf2/gallery/

Web Developer Conference 2013 Seite 7 von 14

Page 8: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

b) Einen Datensatz ausgeben

✔ In PhpStorm bearbeiten: /module/Gallery/src/Gallery/Controller/GalleryController.php

namespace Gallery\Controller;

[...]

class GalleryController extends AbstractActionController{ [...] public function showAction() { $id = $this->params()->fromRoute('id');

$serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('Doctrine\ORM\EntityManager');

$gallery = $objectManager->find('Gallery\Entity\Gallery', $id);

return new ViewModel( array( 'gallery' => $gallery, ) ); }}

✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/show.phtml

<?phpuse Gallery\Entity\Gallery;

$gallery = $this->gallery; /* @var $gallery Gallery */?><h1>Bild anzeigen</h1><table class="table"> <tbody> <tr> <td>ID</td> <td><?php echo $gallery->getId(); ?></td> </tr> <tr> <td>Title</td> <td><?php echo $gallery->getTitle(); ?></td> </tr> <tr> <td>ThumbUrl</td> <td><?php echo $gallery->getThumburl(); ?></td> </tr> <tr> <td>BigUrl</td> <td><?php echo $gallery->getBigurl(); ?></td> </tr> </tbody></table><hr><a href="<?php echo $this->url('gallery') ?>">Gallery</a>

✔ Im Browser öffnen: http://wdc-zf2/gallery/

Web Developer Conference 2013 Seite 8 von 14

Page 9: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

c) Einen Datensatz löschen

✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/index.phtml

<?php foreach ($this->galleryList as $gallery) : /* @var $gallery Gallery */ ?> [...] <?php $update = array('action' => 'update', 'id' => $gallery->getId()); ?> <?php $delete = array('action' => 'delete', 'id' => $gallery->getId()); ?> <tr> [...] <td><a href="<?php echo $this->url('gallery/action', $update) ?>"> ändern </a></td> <td><a href="<?php echo $this->url('gallery/action', $delete) ?>"> löschen </a></td> </tr> <?php endforeach; ?>

✔ In PhpStorm bearbeiten: /module/Gallery/src/Gallery/Controller/GalleryController.php

namespace Gallery\Controller;

[...]

class GalleryController extends AbstractActionController{ [...] public function deleteAction() { $id = $this->params()->fromRoute('id');

$serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('Doctrine\ORM\EntityManager');

$gallery = $objectManager->find('Gallery\Entity\Gallery', $id);

$objectManager->remove($gallery); $objectManager->flush();

return $this->redirect()->toRoute('gallery'); } [...]}

✔ Im Browser öffnen: http://wdc-zf2/gallery/

Web Developer Conference 2013 Seite 9 von 14

Page 10: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

d) Einen Datensatz mit Formular anlegen

✔ In PhpStorm erstellen: /module/Gallery/src/Gallery/Form/GalleryForm.php

<?phpnamespace Gallery\Form;

use Zend\Form\Form;

class GalleryForm extends Form{ public function init() { $this->add( array( 'name' => 'id', 'type' => 'hidden', ) );

$this->add( array( 'name' => 'title', 'type' => 'text', 'options' => array('label' => 'Titel'), 'attributes' => array('class' => 'span5'), ) );

$this->add( array( 'name' => 'thumburl', 'type' => 'file', 'options' => array('label' => 'Thumb'), 'attributes' => array('class' => 'span5'), ) );

$this->add( array( 'name' => 'bigurl', 'type' => 'file', 'options' => array('label' => 'Bild'), 'attributes' => array('class' => 'span5'), ) );

$this->add( array( 'type' => 'Submit', 'name' => 'save', 'attributes' => array( 'value' => 'Speichern', 'id' => 'save', 'class' => 'btn btn-primary', ), ) ); }}

Web Developer Conference 2013 Seite 10 von 14

Page 11: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

✔ In PhpStorm bearbeiten: /module/Gallery/config/module.config.php

<?phpreturn array( [...]

'form_elements' => array( 'invokables' => array( 'Gallery' => 'Gallery\Form\GalleryForm', ), ),

[...] );

✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/create.phtml

<?phpuse Gallery\Form\GalleryForm;

$form = $this->form; /* @var $form GalleryForm */$form->prepare();$form->setAttribute( 'action', $this->url('gallery/action', array('action', 'create'), true));?><h1>Bild anlegen</h1><?php echo $this->form()->openTag($form); foreach ($form as $element) { echo '<div>' . $this->formRow($element) . '</div>'; } echo $this->form()->closeTag();?><hr><a href="<?php echo $this->url('gallery') ?>">Gallery</a>

✔ In PhpStorm bearbeiten: /module/Gallery/src/Gallery/Controller/GalleryController.php

<?phpnamespace Gallery\Controller;

class GalleryController extends AbstractActionController{ [...] public function createAction() { $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('Doctrine\ORM\EntityManager'); $formManager = $serviceManager->get('FormElementManager'); $request = $this->getRequest();

if ($request->isPost()) { $postData = $request->getPost()->toArray(); $fileData = $request->getFiles()->toArray();

$imageDir = realpath(__DIR__ . '/../../../../../public');

$gallery = new Gallery();

if ($fileData['thumburl']['error'] == 0) { $thumburl = '/img/gallery/' . $fileData['thumburl']['name'];

move_uploaded_file(

Web Developer Conference 2013 Seite 11 von 14

Page 12: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

$fileData['thumburl']['tmp_name'], $imageDir . $thumburl );

$gallery->setThumburl($thumburl); }

if ($fileData['bigurl']['error'] == 0) { $bigurl = '/img/gallery/' . $fileData['bigurl' ]['name'];

move_uploaded_file( $fileData['bigurl' ]['tmp_name'], $imageDir . $bigurl );

$gallery->setBigurl($bigurl); }

$title = $postData['title'];

$gallery->setTitle($title);

$objectManager->persist($gallery); $objectManager->flush();

return $this->redirect()->toRoute('gallery'); }

$galleryForm = $formManager->get('Gallery');

return new ViewModel( array( 'form' => $galleryForm, ) ); } [...]}

✔ Im Browser öffnen: http://wdc-zf2/gallery/create/

Web Developer Conference 2013 Seite 12 von 14

Page 13: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

e) Einen Datensatz ändern

✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/update.phtml

<?phpuse Gallery\Form\GalleryForm;use Gallery\Entity\Gallery;

$gallery = $this->gallery; /* @var $gallery Gallery */

$form = $this->form; /* @var $form GalleryForm */$form->prepare();$form->setAttribute( 'action', $this->url( 'gallery/action', array('action' => 'update', 'id' => $gallery->getId()), true ));?><h1>Bild ändern</h1><?php echo $this->form()->openTag($form); foreach ($form as $element) { echo '<div>' . $this->formRow($element) . '</div>'; } echo $this->form()->closeTag();?><hr><a href="<?php echo $this->url('gallery') ?>">Gallery</a>

✔ In PhpStorm bearbeiten: /module/Gallery/src/Gallery/Controller/GalleryController.php

<?phpnamespace Gallery\Controller;

class GalleryController extends AbstractActionController{ [...]

public function updateAction() { $id = $this->params()->fromRoute('id');

$serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('Doctrine\ORM\EntityManager'); $formManager = $serviceManager->get('FormElementManager'); $request = $this->getRequest();

$gallery = $objectManager->find('Gallery\Entity\Gallery', $id);

if ($request->isPost()) { $postData = $request->getPost()->toArray(); $fileData = $request->getFiles()->toArray();

$imageDir = realpath(__DIR__ . '/../../../../../public');

if ($fileData['thumburl']['error'] == 0) { $thumburl = '/img/gallery/' . $fileData['thumburl']['name'];

move_uploaded_file( $fileData['thumburl']['tmp_name'], $imageDir . $thumburl );

$gallery->setThumburl($thumburl);

Web Developer Conference 2013 Seite 13 von 14

Page 14: Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert

}

if ($fileData['bigurl']['error'] == 0) { $bigurl = '/img/gallery/' . $fileData['bigurl' ]['name'];

move_uploaded_file( $fileData['bigurl' ]['tmp_name'], $imageDir . $bigurl );

$gallery->setBigurl($bigurl); }

$title = $postData['title'];

$gallery->setTitle($title);

$objectManager->flush();

return $this->redirect()->toRoute('gallery'); }

$galleryForm = $formManager->get('Gallery'); $galleryForm->get('id')->setValue($gallery->getId()); $galleryForm->get('title')->setValue($gallery->getTitle());

return new ViewModel( array( 'form' => $galleryForm, 'gallery' => $gallery, ) ); }

[...]}

✔ Im Browser öffnen: http://wdc-zf2/gallery/

Web Developer Conference 2013 Seite 14 von 14