controllers.php 921 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. use Silex\Application;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. const BACKBONE = '/client/index.html';
  6. $app->get('/', function (Application $app) {
  7. return $app->redirect(BACKBONE);
  8. });
  9. $app->get('/server', function (Application $app) {
  10. return $app->redirect(BACKBONE);
  11. });
  12. $app->get('/todos/{id}', function (Application $app, int $id) {
  13. /** @var \Model $model */
  14. $model = $app['model'];
  15. $todos = $model->load();
  16. $todo = $todos[$id] ?? NULL;
  17. return $app->json($todo);
  18. });
  19. $app->put('/todos/{id}', function (Application $app, Request $request, int $id) {
  20. /** @var \Model $model */
  21. $model = $app['model'];
  22. $data = json_decode($request->getContent(), true);
  23. $created = $model->save($data);
  24. return new Response('Saved', $created ? Response::HTTP_CREATED : Response::HTTP_OK);
  25. });
  26. $app->match('{url}', function ($url) {
  27. var_dump($url);
  28. });