I have two routes and want to match both routes when some parameter exists in request.
Route 1:
'companies' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id]',
'defaults' => [
'controller' => V1\Rest\Controller\CompaniesController::class,
]
],
'priority' => 2,
'may_terminate' => true,
],
Route 2:
'company_members' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id][/:members][/:member_id]',
'defaults' => [
'controller' => V1\Rest\Controller\CompanyMembersController::class,
]
],
'priority' => 2,
'may_terminate' => true,
],
I want to use CompanyMembersController when members exists in the request and CompaniesController when members doesnt exists .But it is not working.
Your issue is in the second route where you defined members as a parameter [/:members]. You should change this to a /members.
I also would recommend to use constraints for your route parameters. Your routes should look like:
Also you can see the constraints to parameters id & member_id to integers.
References