Skip to content

Working JSON example (php side) #1

Open
wants to merge 1 commit into from
View
70 Module.php
@@ -19,4 +19,74 @@ public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
+
+ public function onBootstrap($e)
+ {
+ $target = $e->getTarget();
+ $events = $target->events();
+ $shared = $events->getSharedManager();
+ $shared->attach('Zend\Stdlib\DispatchableInterface', 'dispatch',
+ array($this, 'onDispatchEnd'), -100);
+ }
+
+ public function onDispatchEnd($e)
+ {
+ $controller = $e->getTarget();
+ $class = get_class($controller);
+
+ if (!preg_match('/^PhlyPeep/', $class)) {
+ return;
+ }
+
+ $viewModel = $e->getResult();
+
+ if (!$viewModel instanceof \Zend\View\Model\ModelInterface) {
+ return;
+ }
+
+ if (!isset($viewModel->peeps)) {
+ return;
+ }
+
+ $peeps = $viewModel->peeps;
+
+ if (!$peeps instanceof \Zend\Paginator\Paginator) {
+ return;
+ }
+
+ $request = $e->getRequest();
+ $headers = $request->headers();
+
+ if (!$headers->has('Accept')) {
+ return;
+ }
+
+ $hasJson = false;
+ $accept = $headers->get('Accept');
+ foreach ($accept->getPrioritized() as $mediaType) {
+ if (0 === strpos($mediaType, 'application/json')) {
+ // application/json Accept header found
+ $hasJson = true;
+ break;
+ }
+ }
+
+ if (!$hasJson) {
+ return;
+ }
+
+ $peeps->getItemsByPage(1);
+
+
+
+ $peepsArray = array();
+
+ foreach ($peeps as $peep) {
+ $peepsArray[] = $peep->getArrayCopy();
+ }
+
+ $viewModel->setVariable('peeps', $peepsArray);
+ }
+
+
}
View
1 src/PhlyPeep/Controller/PeepController.php
@@ -29,6 +29,7 @@ public function indexAction()
$page = $request->query()->get('page', 1);
return array(
'peeps' => $this->service->fetchTimeline($page),
+ 'test' => 'some result',
);
}
View
8 src/PhlyPeep/Model/PeepPaginator.php
@@ -37,11 +37,15 @@ public function count()
public function getItems($offset, $itemCountPerPage)
{
+ $result = null;
switch ($this->timelineType) {
case self::TYPE_ALL:
- return $this->table->fetchTimeline($offset, $itemCountPerPage);
+ $result = $this->table->fetchTimeline($offset, $itemCountPerPage);
+ break;
case self::TYPE_USER:
- return $this->table->fetchUserTimeline($this->user, $offset, $itemCountPerPage);
+ $result = $this->table->fetchUserTimeline($this->user, $offset, $itemCountPerPage);
+ break;
}
+ return $result;
}
}
Something went wrong with that request. Please try again.