Get started with Symfony 6 for beginners — Part 2 | Route, Controller, Rendering.
In the last article, we have been successfully launched the Symfony application for the first time. In this part, we will take a look at the MCV concept and create our first Controller, Route, and view file with just one line.
What is MVC?
MVC (Model View Controller) is an architectural pattern that separates applications into 3 main logical components.
- Model: corresponds to all the data-related logic that the user works with. Retrieving data, the crud operations are done in the Model object.
- View: is responsible for the UI logic of the application.
- Controller: Act as an interface between Model and View components to process all the business logic and incoming requests, manipulate data using the Model component and interact with the Views to render the final output.
Let's create a Controller route to the specified address and will render the view page. We will use the Symfony CLI tool to create our first-ever controller. As always open the command prompt and go to the (cd: change directory) folder the Symfony project lives, in this tutorial it is the my_project folder, and type the line below:
symfony console make:controller FirstController
FirstController is the name of the controller. For more information click here.
We should have seen a success message if everything is alright. As we saw that there are 2 files created: “FirstController.php” and “index.html.twig”. The controller and a view page of it.
If we open the FirstController.php int src/Controller/ folder we will see something like this:
“ #[Route(‘/first’, name: ‘app_first’)] ”is the router that says if we go to /first address we will render “index.html.twig”, which lives int templates/first/ folder. So, let's do it.
First, start on the Symfony server.