This is a very simple post but I sometimes hear people asking how to assign different URL patterns to different controllers so see example below for small demonstration. It differentiates API, home and documentation endpoints.


Endpoints


$ bin/console debug:router
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
app_country_index GET ANY ANY /api/v1/countries
app_message_index POST ANY ANY /api/v1/messages/{type}
home GET ANY ANY /
documentation GET ANY ANY /documentation
------------------- -------- -------- ------ --------------------------

Route files


This is responsible for /api/v1 endpoints.


config/routes/annotations.yaml


controllers:
resource: ../../src/Controller/
type: annotation
prefix: /api/v1

config/routes.yaml


This is responsible for other endpoints.


home:
path: /
methods: [GET]
controller: App\Controller\HomeController::index

documentation:
path: /documentation
methods: [GET]
controller: App\Controller\DocumentationController::index

Controller


CountryController


/**
* @Route("/countries")
*/
class CountryController
{
/**
* @Route("")
* @Method({"GET"})
*/
public function index()
{
}
}

MessageController


/**
* @Route("/messages")
*/
class MessageController
{
/**
* @Route("/{type}")
* @Method({"POST"})
*/
public function index(Request $request, string $type)
{
}
}

HomeController


class HomeController
{
public function index()
{
}
}

DocumentationController


class DocumentationController
{
public function index()
{
}
}