Blog

Getting started with Symfony 4: Routes and Controllers

December 25, 2018
Symfony Routes and Controllers

To create your first page with Symfony, you first need to understand what Route and a Controller is.

Symfony embraces the HTTP Request-Response lifecycle.

The Route is basically a map from a URL path to a controller and it can be configured in 4 different ways: using annotations or configuring route inside YAML , XML and PHP file.

There are not performance difference, use the one you prefer the most, but keep in mind that annotations and yaml configurations of the routes, are the most common across the web.

Controller is a function we write to handle request and return response. The Request can be a form data, URLparameters, etc. Inside a controller, you can do database queries, render HTML, respond in API format, redirect to another controller or return any other format supported by a browser . Keep in mind, that you have to follow one rule. The controller always must return a Symfony response object.

Saying this, you can see that Symfony applications are basically route-controller system.

Let’s create our first page using project we started on previous blog. Run your application using PHP built-in server  php -S 127.0.0.1:8000 -t public  or using  bin/console server:start  to run the application using the Web Server Bundle.

To be able to run second command, you need first to install Web Server Bundle, by executing  composer require symfony/web-server-bundle –dev  inside your project directory.

If open your browser and look at 127.0.0.1:8000

Symfony Demo
http://127.0.0.1:8000/

You see “Welcome to Symfony 4.2.0”. This page is shown, when no  /  route is configured.

Creating a page consist of two steps

  • Create a Route (defining the URL for a page). Example: domain.com/blog/my-first-blog
  • Create a Controller (function that we write, that build the content of the page).

Let’s define our first route. Go to project config directory  cd name_your_project/config  and open  routes.yaml  file.

Uncomment it and change DefaultController to BlogController.

  • index is the name of a Route
  • path is a URL
  • controller is a PHP class, with an index as a method inside the class, which is responsible for building the page 

Next create new PHP class inside  src/Controller , and name it BlogController. Add namespace above the class if it’s not automatically added by the IDE  namespace App\Controller; 

Inside BlogController class, create public function index, and return Symfony return object.

If you refresh your page, you will be able to see “Hello from index method!”;

To be able to configure Route using annotations, you will have to add support for them by running the command  composer require annotations  inside your project. Next, comment out everything inside routes.yaml, and go to BlogController. Above the method, add @Route annotation, with  (“/”, name=”index”) . Check if you have use statement above the class for Annotation Route. If not, add it above the class use Symfony\Component\Routing\Annotation\Route;  Refresh the page and you will see the same result as before. 

If you are wondering where the Symfony Profiler went, just add html tag wrapped around the text inside response, and you will be able to see Profiler again.   

If you are wondering how the  /blog/welcome-to-blog  routes are created, let’s create one. Using wildcard we can create dynamic URL. Create new method named blog, add @Route annotation as you want and add wildcard to it. 

Now if you open your browser and check http://127.0.0.1:8000/blog/welcome-to-blog page, you will see

Symfony Slug Response

As I have already mention, you can render HTML template, but I will cover that topic in future.

You Might Also Like

No Comments

Leave a Reply