Skip to content

Commit f2179a4

Browse files
committed
step10 - Json Responses + Route Generation
1 parent 67c2ec8 commit f2179a4

File tree

2 files changed

+78
-49
lines changed

2 files changed

+78
-49
lines changed

.idea/workspace.xml

+48-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AppBundle/Controller/GenusController.php

+30-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace AppBundle\Controller;
44

55
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6-
6+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
77
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8+
use Symfony\Component\HttpFoundation\JsonResponse;
89
use Symfony\Component\HttpFoundation\Response;
910

1011
class GenusController extends Controller
@@ -14,17 +15,39 @@ class GenusController extends Controller
1415
*/
1516
public function showAction($genusName)
1617
{
17-
$notes = [
18-
'Octopus asked me a riddle, outsmarted me',
19-
'I counted 8 legs... as they wrapped around me',
20-
'Inked!'
21-
];
18+
////// $notes = [
19+
////// 'Octopus asked me a riddle, outsmarted me',
20+
////// 'I counted 8 legs... as they wrapped around me',
21+
////// 'Inked!'
22+
////// ];
23+
24+
2225
//// return new Response("Under the sea!!!! Oh Yeah!!! <br/> The genus: ".$genusName);
2326
// $templating = $this->container->get('templating');
2427
//// $this->container->get('templating')
2528
// $html = $templating->render('genus/show.html.twig', array('name' => $genusName));
2629
//
2730
// return new Response($html);
28-
return $this->render('genus/show.html.twig', array('name' => $genusName, 'notes' => $notes));
31+
return $this->render('genus/show.html.twig', array('name' => $genusName));
32+
}
33+
34+
/**
35+
* @Route("/genus/{genusName}/notes")
36+
* @Method("GET")
37+
*/
38+
public function getNotesAction($genusName)
39+
{
40+
$notes = [
41+
['id' => 1, 'username' => 'AquaPelham', 'avatarUri' => '/images/leanna.jpeg', 'note' => 'Octopus asked me a riddle, outsmarted me', 'date' => 'Dec. 10, 2015'],
42+
['id' => 2, 'username' => 'AquaWeaver', 'avatarUri' => '/images/ryan.jpeg', 'note' => 'I counted 8 legs... as they wrapped around me', 'date' => 'Dec. 1, 2015'],
43+
['id' => 3, 'username' => 'AquaPelham', 'avatarUri' => '/images/leanna.jpeg', 'note' => 'Inked!', 'date' => 'Aug. 20, 2015'],
44+
];
45+
46+
$data = [
47+
'notes' => $notes
48+
];
49+
50+
// return new Response(json_encode($data)); //está retornando os dados como json
51+
return new JsonResponse($data); //também retorna os dados como json
2952
}
3053
}

0 commit comments

Comments
 (0)