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
4. 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
5. 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
6. 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
7. 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
8. 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
9. 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