14/04/2018 - SYMFONY
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.
$ 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
------------------- -------- -------- ------ --------------------------
This is responsible for /api/v1
endpoints.
controllers:
resource: ../../src/Controller/
type: annotation
prefix: /api/v1
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
/**
* @Route("/countries")
*/
class CountryController
{
/**
* @Route("")
* @Method({"GET"})
*/
public function index()
{
}
}
/**
* @Route("/messages")
*/
class MessageController
{
/**
* @Route("/{type}")
* @Method({"POST"})
*/
public function index(Request $request, string $type)
{
}
}
class HomeController
{
public function index()
{
}
}
class DocumentationController
{
public function index()
{
}
}