Answers.Body: <p>You can make a capturing group optional, simply append <code>?</code> to it. In this case you'd need to add an additional group that includes the backslash and make it optional.</p>
  
  <pre><code>"myplugin(/(?P&lt;myVariable&gt;[-\w]+))?" =&gt; array("action" =&gt; "myPlugin/myAction")
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-08T10:47:14.570Z
Answers.Text: Thank you – I also edited my answer to http://craftcms.stackexchange.com/questions/8524/route-that-should-be-matching-numbers-not-matching-a-zero/8552#8552 as this would solve that issue nicely as well.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-08T10:34:26.953Z
Answers.LastActivityDate: 2015-03-08T10:34:26.953Z
Answers.UserDisplayName: carlcs
Body: <p>Is it possible to have optional route segments/variables with getCpRoutes() and getSiteRoutes()?</p>
  
  <p>I'd like the following route to work, even if the URI only contains one segment ("myplugin") rather than two ("myplugin/somevalue"):</p>
  
  <pre><code>"myplugin/(?P&lt;myVariable&gt;[-\w]+)" =&gt; array( "action" =&gt; "myPlugin/myAction" )
  </code></pre>
  
  <p>How would the regexp look? Is it even possible, or do I need another route pointing to the same action, but omitting the <em>myVariable</em> segment?</p>
  
  <p>This question is kind of a follow up to <a href="https://craftcms.stackexchange.com/questions/8524/route-that-should-be-matching-numbers-not-matching-a-zero/8552#8552">Route that should be matching numbers not matching a zero?</a></p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-03-08T10:22:21.130Z
Comments.Text: Did you try to simply make that capturing group optional `myplugin/(?P<myVariable>[-\w]+)?` or `myplugin(/(?P<myVariable>[-\w]+))?`?
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-03-08T10:27:45.353Z
Comments.Text: I did not, and the latter suggestion (```myplugin(/(?P<myVariable>[-\w]+))?```) works! Thanks a lot – feel free to add your comment as an answer.
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Tags: <plugin-development><routes>
Title: Is it possible to define optional route segments?
ThreadText: # Is it possible to define optional route segments?
  
  (Asked by: username_0 on 2015-03-08)
  
  Is it possible to have optional route segments/variables with getCpRoutes() and getSiteRoutes()?
  
  I'd like the following route to work, even if the URI only contains one segment ("myplugin") rather than two ("myplugin/somevalue"):
  
  ```
  "myplugin/(?P<myVariable>[-\w]+)" => array( "action" => "myPlugin/myAction" )
  
  ```
  
  How would the regexp look? Is it even possible, or do I need another route pointing to the same action, but omitting the *myVariable* segment?
  
  This question is kind of a follow up to [Route that should be matching numbers not matching a zero?](https://craftcms.stackexchange.com/questions/8524/route-that-should-be-matching-numbers-not-matching-a-zero/8552#8552)
  
  **Tags:** plugin-development, routes
  
  > **Comments:**
  > **username_1**: Did you try to simply make that capturing group optional `myplugin/(?P<myVariable>[-\w]+)?` or `myplugin(/(?P<myVariable>[-\w]+))?`?  
  > **username_0**: I did not, and the latter suggestion (```myplugin(/(?P<myVariable>[-\w]+))?```) works! Thanks a lot – feel free to add your comment as an answer.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-03-08. Score: 4 | Accepted answer
  
  You can make a capturing group optional, simply append `?` to it. In this case you'd need to add an additional group that includes the backslash and make it optional.
  
  ```
  "myplugin(/(?P<myVariable>[-\w]+))?" => array("action" => "myPlugin/myAction")
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you – I also edited my answer to http://craftcms.stackexchange.com/questions/8524/route-that-should-be-matching-numbers-not-matching-a-zero/8552#8552 as this would solve that issue nicely as well.  
  
  ---
  
  