• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"
 

Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"

on

  • 3,005 views

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

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

Statistics

Views

Total Views
3,005
Views on SlideShare
2,994
Embed Views
11

Actions

Likes
0
Downloads
3
Comments
0

1 Embed 11

http://lanyrd.com 11

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks" Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks" Document Transcript

    • 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
    • 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 <?php return array( 'doctrine' => array( 'connection' => array( 'orm_default' => array( 'driverClass' => 'DoctrineDBALDriverPDOSqliteDriver', '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
    • 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 <?php return array( 'router' => array( 'routes' => array( 'gallery' => array( 'type' => 'Literal', 'options' => array( 'route' => '/gallery', 'defaults' => array( 'controller' => 'GalleryControllerGallery', '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( 'GalleryControllerGallery' => 'GalleryControllerGalleryController', ), ), 'view_manager' => array( 'template_path_stack' => array( __DIR__ . '/../view', ), ), ); ✔ Im Browser öffnen: http://wdc-zf2/gallery/ Web Developer Conference 2013 Seite 3 von 14
    • Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert b) Gallery Entity anlegen ✔ In PhpStorm erstellen: /module/Gallery/src/Gallery/Entity/Gallery.php <?php namespace GalleryEntity; use DoctrineORMMapping as ORM; /** * @ORMEntity * @ORMTable(name="gallery") */ class Gallery{ /** * @ORMId * @ORMGeneratedValue(strategy="AUTO") * @ORMColumn(type="integer") */ protected $id; /** @ORMColumn(type="string") */ protected $title; /** @ORMColumn(type="string") */ protected $thumburl; /** @ORMColumn(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
    • Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert ✔ In PhpStorm bearbeiten: /module/Gallery/config/module.config.php <?php return array( [...] 'doctrine' => array( 'driver' => array( 'gallery_entities' => array( 'class' => 'DoctrineORMMappingDriverAnnotationDriver', 'cache' => 'array', 'paths' => array(__DIR__ . '/../src/Gallery/Entity') ), 'orm_default' => array( 'drivers' => array( 'GalleryEntity' => '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 <?php namespace GalleryController; use use use use GalleryEntityGallery; ZendMathRand; ZendMvcControllerAbstractActionController; ZendViewModelViewModel; class GalleryController extends AbstractActionController { public function createAction() { $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('DoctrineORMEntityManager'); $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
    • Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert ✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/create.phtml <?php ZendDebugDebug::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
    • 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 GalleryController; [...] class GalleryController extends AbstractActionController { public function indexAction() { $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('DoctrineORMEntityManager'); $repository = $objectManager->getRepository('GalleryEntityGallery'); $galleryList = $repository->findAll(); return new ViewModel( array( 'galleryList' => $galleryList, ) ); } [...] } ✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/index.phtml <?php use GalleryEntityGallery; $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
    • 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 GalleryController; [...] class GalleryController extends AbstractActionController { [...] public function showAction() { $id = $this->params()->fromRoute('id'); $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('DoctrineORMEntityManager'); $gallery = $objectManager->find('GalleryEntityGallery', $id); return new ViewModel( array( 'gallery' => $gallery, ) ); } } ✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/show.phtml <?php use GalleryEntityGallery; $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
    • 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 GalleryController; [...] class GalleryController extends AbstractActionController { [...] public function deleteAction() { $id = $this->params()->fromRoute('id'); $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('DoctrineORMEntityManager'); $gallery = $objectManager->find('GalleryEntityGallery', $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
    • 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 <?php namespace GalleryForm; use ZendFormForm; class GalleryForm extends Form { public function init() { $this->add( array( 'name' => 'id', 'type' => 'hidden', ) ); $this->add( array( 'name' 'type' 'options' 'attributes' ) ); => => => => 'title', 'text', array('label' => 'Titel'), array('class' => 'span5'), $this->add( array( 'name' 'type' 'options' 'attributes' ) ); => => => => 'thumburl', 'file', array('label' => 'Thumb'), array('class' => 'span5'), $this->add( array( 'name' 'type' 'options' 'attributes' ) ); => => => => 'bigurl', 'file', array('label' => 'Bild'), 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
    • Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert ✔ In PhpStorm bearbeiten: /module/Gallery/config/module.config.php <?php return array( [...] 'form_elements' => array( 'invokables' => array( 'Gallery' => 'GalleryFormGalleryForm', ), ), [...] ); ✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/create.phtml <?php use GalleryFormGalleryForm; $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 <?php namespace GalleryController; class GalleryController extends AbstractActionController { [...] public function createAction() { $serviceManager = $this->getServiceLocator(); $objectManager = $serviceManager->get('DoctrineORMEntityManager'); $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
    • 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
    • Drehbuch zum Talk »Rapid Prototyping mit PHP Frameworks« Ralf Eggert e) Einen Datensatz ändern ✔ In PhpStorm bearbeiten: /module/Gallery/view/gallery/gallery/update.phtml <?php use GalleryFormGalleryForm; use GalleryEntityGallery; $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 <?php namespace GalleryController; class GalleryController extends AbstractActionController { [...] public function updateAction() { $id = $this->params()->fromRoute('id'); $serviceManager $objectManager $formManager $request = = = = $this->getServiceLocator(); $serviceManager->get('DoctrineORMEntityManager'); $serviceManager->get('FormElementManager'); $this->getRequest(); $gallery = $objectManager->find('GalleryEntityGallery', $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
    • 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