Routing in the Symfony framework (Lesson 6: Routing)

After learning how to create controllers (here), it is important to understand how a user can invoke a controller and how to work with it.

The Symfony framework has a special feature called routing system. In yii framework , for example, routing is determined by the file structure of the application by default (although you can still use a routing system with the necessary tools). However, in Symfony framework, you have to define the routing manually (excluding the basic routing created by the bundle generator).

So, the base file that defines the routing is app/config/routing.yml. But this file shouldn't usually define the paths of your application - it just needs to load the routing file from your bundle. For example,

# app/config/routing.yml
acme_hello:
   resource: "@AcmeHelloBundle/Resources/config/routing.yml"
Now, let's talk about the routing itself.

The first line contains the route name. If you are using the yml format, it doesn't affect anything, it's just for convenience. The second line tells the router to load the routing file from your bundle, located at the corresponding address.

In our example, all the information about routes is contained in src/path_to_bundle/Resources/config/routing.yml

So, let's assume that the user enters something like http://site.ru/blog in the address bar. In order for this action to invoke the actionBlog method in the controller, simply:

# app/config/routing.yml
 blog_show:
   path: /blog
   defaults: { _controller: AcmeBlogBundle:Blog:show }
In the given fragment, we tell the router to pass requests in the form site.net/blog to the BlogController, showAction method. Note that in this format, the words Controller and Action are not included in the controller and action route. defaults specifies the default parameters. Here we specify the value of the _controller parameter, which determines which controller Symfony framework will use to handle the request.

Parameters can be passed as follows: For example, if you want to pass the name parameter, the address will look like site.net/blog/name. You can specify this to the router as follows:

# app/config/routing.yml
 blog_show:
   path: /blog/{name}
   defaults: { _controller: AcmeBlogBundle:Blog:show }
Thus, we have defined a parameter for the showAction($name) method. As you have already guessed, you can set the default value for this parameter in defaults, and then it will not be required:
//...
defaults: { _controller: AcmeBlogBundle:Blog:show, name: user }
This way, for example, pagination (breaking into pages) and much more can be implemented.

In addition, you can specify the required parameter type for the Symfony framework controller (for example, for pagination) using a regular expression:

blog:
  path: /blog/{page}
  defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
  requirements:
    page: \d+
The specified fragment allows only integer values for the page parameter.

You can also explicitly specify the allowed values for a parameter. For example,

homepage:
  path: /{culture}
  defaults: { _controller: AcmeDemoBundle:Main:homepage, culture: en }
  requirements:
    culture: en|fr
You can also specify the request type:
homepage:
  path: /{culture}
  defaults: { _controller: AcmeDemoBundle:Main:homepage, culture: en }
  requirements:
    culture: en|fr
  methods: [GET]
To view the current routes of the system, enter the following in the console:
php app/console router:debug
Important! After changing the routing configuration, you need to clear the cache (otherwise, you will not see the changes):
php app/console cache:clean --env=prod --no-debug
(for production build)