Define Optional Placeholder Tokens
3.xYou can make path segments optional using the notation {/attribute1,attribute2,...}.
Rules for optional attributes:
- The leading slash separator must be inside the placeholder token (e.g.,
{/year,month}). - Attributes are sequentially optional: you cannot match a later attribute without matching all preceding ones (e.g., you cannot have
monthwithoutyear). - You can only have one set of optional attributes per route.
- Optional attributes should always be placed at the end of the route path.
<?php
// Matches /archive, /archive/1979, /archive/1979/11, etc.
$map->get('archive', '/archive{/year,month,day}')
->tokens([
'year' => '\d{4}',
'month' => '\d{2}',
'day' => '\d{2}',
]);
?>