Witam, próbuję zrobić prosty slider zdjęć który będzie pokazywany w layoucie strony, ścieżka danego zdjęcia ma być ładowana z bazy danych. Jestem początkujący z ZF2, pewnie jest na to sposób, ale niestety nie wiem jaki ponieważ strona w momencie uruchamiania funkcji pobierającej dane się wywala. Oto kod:


Module.php
  1. namespace MyImageSlider;
  2.  
  3. use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
  4. use Zend\Mvc\MvcEvent;
  5. use MyImageSlider\Controller\Slider;
  6. use MyImageSlider\Model\MyImageSlider;
  7. use MyImageSlider\Model\MyImageSliderTable;
  8. use Zend\Db\ResultSet\ResultSet;
  9. use Zend\Db\TableGateway\TableGateway;
  10.  
  11.  
  12. class Module implements AutoloaderProviderInterface
  13. {
  14. public function getAutoloaderConfig()
  15. {
  16. return array(
  17. 'Zend\Loader\ClassMapAutoloader' => array(
  18. __DIR__ . '/autoload_classmap.php',
  19. ),
  20. 'Zend\Loader\StandardAutoloader' => array(
  21. 'namespaces' => array(
  22. // if we're in a namespace deeper than one level we need to fix the \ in the path
  23. __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
  24. ),
  25. ),
  26. );
  27. }
  28.  
  29. public function getConfig()
  30. {
  31. return include __DIR__ . '/config/module.config.php';
  32. }
  33.  
  34. public function getServiceConfig()
  35. {
  36. return array(
  37. 'factories' => array(
  38. 'MyImageSlider\Model\MyImageSliderTable' => function($sm) {
  39. $tableGateway = $sm->get('MyImageSliderTableGateway');
  40. $table = new MyImageSliderTable($tableGateway);
  41. return $table;
  42. },
  43. 'MyImageSliderTableGateway' => function ($sm) {
  44. $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
  45. $resultSetPrototype = new ResultSet();
  46. $resultSetPrototype->setArrayObjectPrototype(new MyImageSlider());
  47. return new TableGateway('rotator', $dbAdapter, null, $resultSetPrototype);
  48. },
  49. ),
  50. );
  51. }
  52.  
  53. public function onBootstrap(MvcEvent $e)
  54. {
  55. $this->e = $e;
  56. $this->ImageSlider($this->e);
  57. }
  58.  
  59. public function ImageSlider($e)
  60. {
  61. $viewModel = $e->getViewModel();
  62. $slider = new Slider();
  63. $viewModel->setVariable('ImageSlider', $slider->images);
  64. }
  65. }


Controller/Slider.php

  1. namespace MyImageSlider\Controller;
  2.  
  3. use Zend\Mvc\Controller\AbstractActionController;
  4.  
  5. class Slider extends AbstractActionController
  6. {
  7. public $images;
  8.  
  9. protected $imageTable;
  10. public function getImageTable()
  11. {
  12. if (!$this->imageTable) {
  13. $sm = $this->getServiceLocator();
  14. $this->imageTable = $sm->get('MyImageSlider\Model\MyImageSliderTable');
  15. }
  16. return $this->imageTable;
  17. }
  18.  
  19. public function __construct()
  20. {
  21. $this->images = $this->getImageTable()->fetchAll();
  22. }
  23. }



Model/MyImageSlider.php

  1. namespace MyImageSlider\Model;
  2.  
  3. class MyImageSlider
  4. {
  5. public $id;
  6. public $imgSrc;
  7. public $desc;
  8.  
  9. public function exchangeArray($data)
  10. {
  11. $this->id = (isset($data['Id'])) ? $data['Id'] : null;
  12. $this->imgSrc = (isset($data['ImgSrc'])) ? $data['ImgSrc'] : null;
  13. $this->desc = (isset($data['Description'])) ? $data['Description'] : null;
  14. }
  15. }


Model/MyImageSliderTable.php

  1. namespace MyImageSlider\Model;
  2.  
  3. use Zend\Db\TableGateway\TableGateway;
  4.  
  5. class MyImageSliderTable
  6. {
  7. protected $tableGateway;
  8.  
  9. public function __construct(TableGateway $tableGateway)
  10. {
  11. $this->tableGateway = $tableGateway;
  12. }
  13.  
  14. public function fetchAll()
  15. {
  16. $resultSet = $this->tableGateway->select();
  17. return $resultSet;
  18. }
  19. }