Answers.Body: <p>Short and sweet... I think you just need to add a second route.</p>
  
  <pre class="lang-php prettyprint-override"><code>'blog/archive' =&gt; 'controller/action/path',
  'blog/archive/&lt;year:\d{4}&gt;' =&gt; 'controller/action/path'
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-24T14:59:22.507Z
Answers.LastActivityDate: 2020-04-24T14:59:22.507Z
Answers.UserDisplayName: Lindsey D
Body: <p>I have a question about custom routes set in <code>routes.php</code>. Can route segment be optional?</p>
  
  <p>Taking example from docs - <code>'blog/archive/&lt;year:\d{4}&gt;' =&gt; 'controller/action/path'</code>, can we make year token optional in url and still match route when someone vists <code>blog/archive</code>?</p>
  
Tags: <routes>
Title: Custom route with optional tokens
ThreadText: # Custom route with optional tokens
  
  (Asked by: username_0 on 2020-04-24)
  
  I have a question about custom routes set in `routes.php`. Can route segment be optional?
  
  Taking example from docs - `'blog/archive/<year:\d{4}>' => 'controller/action/path'`, can we make year token optional in url and still match route when someone vists `blog/archive`?
  
  **Tags:** routes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-04-24. Score: 2 | Accepted answer
  
  Short and sweet... I think you just need to add a second route.
  
  ```php
  'blog/archive' => 'controller/action/path',
  'blog/archive/<year:\d{4}>' => 'controller/action/path'
  
  ```
  
  
  ---
  
  