Skip to content

Instantly share code, notes, and snippets.

@Raistlfiren
Last active April 14, 2016 16:39
Show Gist options
  • Save Raistlfiren/6371ce9378eb964de0e8d7130f653327 to your computer and use it in GitHub Desktop.
Save Raistlfiren/6371ce9378eb964de0e8d7130f653327 to your computer and use it in GitHub Desktop.
<?php
//Create news article routes
$this->get('/create', function(ServerRequestInterface $request, ResponseInterface $response, $args) {
$data = $this->newsHandler->getCreate();
return $this->view->render($response, 'news/show.html.twig', $data);
})->setName('new_create')->add($this->getContainer()->get('csrf'));
$this->post('/create', function(ServerRequestInterface $request, ResponseInterface $response, $args) {
$data = $this->newsHandler->postCreate($request, $response, $this->router);
return $this->view->render($response, 'news/show.html.twig', $data);
})->setName('new_create_save')->add($this->getContainer()->get('csrf'));
//NewsHandler postCreate method
public function postCreate(Request $request, Response $response, Router $router)
{
if ($this->newsValidation->isValid($request)) {
$updateList = $this->newsValidation->getColumnValues($request);
$new = $this->news->create($updateList);
if ($new) {
$success = new FlashEvent('News article was successfully created!');
$this->dispatcher->dispatch(TestEvents::NEWS_UPDATE_COMPLETED, $success);
$response = $response->withRedirect($router->pathFor('new', ['id' => $new]));
return $response;
}
} else {
$errors = $this->newsValidation->getErrors();
$error = new FlashEvent('News article failed to save, please correct the errors.', $errors);
$this->dispatcher->dispatch(TestEvents::NEWS_UPDATE_FAILED, $error);
}
$new = $request->getParsedBody();
$statuses = $this->statuses->findAll();
return ['new' => $new, 'statuses' => $statuses, 'form' => true, 'errors' => $errors];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment