In this lesson, we are going to learn how to set up GET queries using Symfony to fetch data from our ToDo app. By the end of this lesson, you will know how to retrieve all the tasks and also how to get a specific task by its ID.
Goals:
- Understand what GET queries are.
- Learn how to set up and implement GET requests to retrieve data from the ToDo app.
- Practically apply GET requests to get all tasks and get tasks by ID.
GET queries are requests made by the client (like a web browser) to receive data from the server. Think of it as asking a librarian for specific books. The librarian gives you a book based on your request.
Imagine needing a specific book from a librarian. You ask (send a GET
request), and they give you the book (response). Similarly, your ToDo app asks the server for tasks and gets the task data back.
In web applications, GET
requests are critical because they allow users to retrieve and view data, like browsing items in an online store.
Now you understand what GET
queries are and why they are essential for fetching data in web applications. This forms the foundation of our ToDo app.
Our ToDoService manages the application's business logic. It provides methods to retrieve all tasks and find a task by its ID. The service is injected into the controller so that the controller can delegate the task retrieval logic to it.
php1<?php 2 3namespace App\Service; 4 5class ToDoService 6{ 7 private $todos = [ 8 ["id" => 1, "title" => "Learn Symfony", "completed" => false], 9 ["id" => 2, "title" => "Set up REST API", "completed" => false] 10 ]; 11 12 public function findAll(): array 13 { 14 return $this->todos; 15 } 16 17 public function findOne($id) 18 { 19 foreach ($this->todos as $todo) { 20 if ($todo["id"] == $id) { 21 return $todo; 22 } 23 } 24 return null; 25 } 26}
The ToDoService class includes a hardcoded list of tasks and methods to find all tasks or a specific task by ID. This service is responsible for the business logic related to task retrieval and is utilized by the controller to provide data to the client. In usual web applications, a database is used to store data; however, for simplicity, we've used an array to store the ToDo data.
We need a controller to define the routes and manage the requests for our ToDo app. Below is an example of how to set up a ToDoController in Symfony to handle GET requests for all tasks and a specific task by ID.
php1<?php 2 3namespace App\Controller; 4 5use App\Service\ToDoService; 6use Symfony\Component\HttpFoundation\JsonResponse; 7use Symfony\Component\Routing\Annotation\Route; 8use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 9 10class ToDoController extends AbstractController 11{ 12 private $todoService; 13 14 public function __construct(ToDoService $todoService) 15 { 16 $this->todoService = $todoService; 17 } 18 19 /** 20 * @Route("/todos", name="todos", methods={"GET"}) 21 */ 22 public function findAll(): JsonResponse 23 { 24 return new JsonResponse($this->todoService->findAll()); 25 } 26 27 /** 28 * @Route("/todos/{id}", name="todo", methods={"GET"}) 29 */ 30 public function findOne($id): JsonResponse 31 { 32 return new JsonResponse($this->todoService->findOne($id)); 33 } 34}
This ToDoController defines routes and handles HTTP GET requests to fetch all tasks or a task by its ID. It delegates the task retrieval logic to the ToDoService and returns the appropriate JSON responses to the client.
To enable our newly created routes, we need to update the routes configuration in Symfony. This ensures that the application knows how to map HTTP requests to the corresponding controller methods.
YAML1todo_list: 2 path: /todos 3 controller: App\Controller\ToDoController::findAll 4 5todo_show: 6 path: /todos/{id} 7 controller: App\Controller\ToDoController::findOne
Testing ensures that our routes work correctly. You can use a browser or Postman to check the functionality of the routes. This step is crucial to make sure the data retrieval is implemented properly in our ToDo app.
Examples:
- Accessing All Tasks: Open
http://localhost/todos
in a browser to see a JSON array. - Accessing Task by ID: Visit
http://localhost/todos/1
for task details;http://localhost/todos/999
should show a 404 error.
You’ve learned to test GET
queries to ensure data retrieval routes work as expected. This is crucial for making sure our ToDo app functions properly.
You now understand how to set up GET
queries in Symfony, forming the foundation of the ToDo app. Practice implementing these skills to reinforce your learning. Happy coding!