<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>

CC BY-SA 3.0
2015-03-08T10:47:14.570Z
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.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-08T10:34:26.953Z
2015-03-08T10:34:26.953Z
carlcs
<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>

CC BY-SA 3.0
2015-03-08T10:22:21.130Z
Did you try to simply make that capturing group optional `myplugin/(?P<myVariable>[-\w]+)?` or `myplugin(/(?P<myVariable>[-\w]+))?`?
carlcs
CC BY-SA 3.0
2015-03-08T10:27:45.353Z
I did not, and the latter suggestion (```myplugin(/(?P<myVariable>[-\w]+))?```) works! Thanks a lot – feel free to add your comment as an answer.
Mats Mikkel Rummelhoff
<plugin-development><routes>
Is it possible to define optional route segments?
# 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[-\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?

**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[-\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.  

---

