Roman
Roman

Reputation: 2549

Configure Silex Routes with optional parameters

I have configured these 2 routes

$app->match ( '/controller/param/{country}/{q}', "demo.controller:demoAction" )->value ( 'country', 'de' )->value('q', '')->bind ( 'demo_action_with_country' );
$app->match ( '/controller/param/{q}', "demo.controller:demoAction" )->value ( 'q', '' )->bind ( 'demo_action_without_country' );

However - this does not work. If i call /controller/param/Test-String the route matches and returns content. If i call /controller/param/DE/Test-String i get NotFoundHttpException

How can i solve the problem?

Upvotes: 2

Views: 6141

Answers (1)

Roman
Roman

Reputation: 2549

Okay, i could easy fix that by my own. Here is my solution:

$app->match ( '/controller/param/{q}', "demo.controller:demoAction" )->value ( 'q', false )->bind ( 'demo_action_without_country' );
$app->match ( '/controller/param/{country}/{q}', "demo.controller:demoAction" )->value ( 'country', false )->value('q', '')->bind ( 'demo_action_with_country' );

Upvotes: 7

Related Questions