<p>You can't have tokens in your plugin routes, but you can easily add a segment variable that recreates the asterisk token's functionality with some regex. According to the the <a href="http://buildwithcraft.com/docs/routing#available-tokens" rel="nofollow noreferrer">official docs on routing</a>, the asterisk represents</p>

<blockquote>
  <p>(...) any string of characters, except for a forward slash (/)</p>
</blockquote>

<p>Here's what the defintion for a route with a "asterisk-like" segment could look like:</p>

<pre><code>public function registerSiteRoutes()
{
    return array(
        'myroute/(?P&lt;asteriskToken&gt;[-\w]+)' =&gt; array('action' =&gt; 'myPlugin/myAction')
    );    
}
</code></pre>

<p><em>Note that the string "asteriskToken" will be the variable name for this route segment's value when it is passed to your controller. It could be named anything.</em></p>

<p>The above route will match any URIs such as</p>

<p>/myroute/whatever <em>(asterisk token will be "whatever")</em><br>
/myroute/123 <em>(asterisk token will be "123")</em><br>
/myroute//foobar <em>(asterisk token will be "foobar")</em>  </p>

<p>Coming to your controller, here's how you could look up the asterisk token variable's value:</p>

<pre><code>public function actionMyAction( array $variables = array() )
{
   $asteriskToken = $variables[ 'asteriskToken' ];
}
</code></pre>

<p>Be aware that any calls to /myroute (i.e. without the second segment) will now 404. If this is unwanted – or if you want to define a default value for your asterisk variable – you can make the asterisk token segment <em>optional</em>. This is done by wrapping the segment variable in parentheses and appending a question mark at the end (kudos to <a href="https://craftcms.stackexchange.com/users/125/carlcs">@carlcs</a> for this trick, originally given as an <a href="https://craftcms.stackexchange.com/a/8569/1098">answer to my question here</a>:</p>

<pre><code>'myroute(/(?P&lt;asteriskToken&gt;[-\w]+))?' =&gt; array('action' =&gt; 'myPlugin/myAction')
</code></pre>

<p>With the latter regex, your route will now also match the following:</p>

<p>/myroute <em>(asterisk token will be <strong>undefined</strong>)</em></p>

<p>...and in your controller, here's how you could define a default value for the asterisk token variable:</p>

<pre><code>$asteriskToken = isset( $variables[ 'asteriskToken' ] ) ? $variables[ 'asteriskToken' ] : 'myDefaultAsteriskValue';
</code></pre>

CC BY-SA 3.0
2015-03-19T05:33:05.507Z
Worked like a charm. Thanks for both answering it and also answering what I probably would've run into afterwards. You're the man!
taylor
CC BY-SA 3.0
2015-03-19T14:59:52.237Z
Happy to be able to help out, @ZackTaylor :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-19T19:30:59.483Z
So I did run into one issue, which is when the route has special characters in it (even when running through url_encode() first.) For example, route 'myroute/check-this-out' works fine, but if it's 'myroute/check-this-out%21', it leads to a 404. When using the "asterisk token" in CP, this is not an issue and does not lead to a 404. Any ideas how to get around this?
taylor
CC BY-SA 3.0
2015-03-19T23:34:10.057Z
Hi @ZackTaylor, I notice you've created a new thread for the question in your above comment. Here's the link, as a followup: http://craftcms.stackexchange.com/questions/8754/dynamic-routing-via-plugin-with-special-characters/8760#8760
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-18T23:50:53.137Z
2015-03-19T00:05:54.180Z
2017-04-13T12:51:00.253Z
Mats Mikkel Rummelhoff
<p>I'd like to use the asterisk token functionality that is available in the CP in my plugin. This way I can have an infinite amount of 'pages' under certain routes. In the CP, it looks like <strong>myroute/*</strong></p>

<p>Is it possible for me to get the same functionality in my plugin routes?</p>

<p>Routes have been working amazing for me via the standard:</p>

<pre><code>public function registerSiteRoutes()
{
    return array(
    'myroute' =&gt; array('action' =&gt; 'myPlugin/myAction')
  );    
}
</code></pre>

<p>Though when I try the asterisk trick, it doesn't work:</p>

<pre><code>public function registerSiteRoutes()
{
    return array(
    'myroute/*' =&gt; array('action' =&gt; 'myPlugin/myAction')
  );    
}
</code></pre>

<p>Does anyone know if this is possible and/or the way to accomplish this?</p>

<p>Thanks!</p>

<plugin-development><routes><php><token>
Plugin Routes: Asterisk Token effect from CP Routes
# Plugin Routes: Asterisk Token effect from CP Routes

(Asked by: username_0 on 2015-03-18)

I'd like to use the asterisk token functionality that is available in the CP in my plugin. This way I can have an infinite amount of 'pages' under certain routes. In the CP, it looks like myroute/*
Is it possible for me to get the same functionality in my plugin routes?
Routes have been working amazing for me via the standard:
public function registerSiteRoutes()
{
    return array(
    'myroute' => array('action' => 'myPlugin/myAction')
  );    
}

Though when I try the asterisk trick, it doesn't work:
public function registerSiteRoutes()
{
    return array(
    'myroute/*' => array('action' => 'myPlugin/myAction')
  );    
}

Does anyone know if this is possible and/or the way to accomplish this?
Thanks!

**Tags:** plugin-development, routes, php, token


---

## 1 answers

### Answer by username_1 on 2015-03-18. Score: 6 | Accepted answer

You can't have tokens in your plugin routes, but you can easily add a segment variable that recreates the asterisk token's functionality with some regex. According to the the official docs on routing, the asterisk represents

(...) any string of characters, except for a forward slash (/)

Here's what the defintion for a route with a "asterisk-like" segment could look like:
public function registerSiteRoutes()
{
    return array(
        'myroute/(?P[-\w]+)' => array('action' => 'myPlugin/myAction')
    );    
}

Note that the string "asteriskToken" will be the variable name for this route segment's value when it is passed to your controller. It could be named anything.
The above route will match any URIs such as
/myroute/whatever (asterisk token will be "whatever")
/myroute/123 (asterisk token will be "123")
/myroute//foobar (asterisk token will be "foobar") 
Coming to your controller, here's how you could look up the asterisk token variable's value:
public function actionMyAction( array $variables = array() )
{
   $asteriskToken = $variables[ 'asteriskToken' ];
}

Be aware that any calls to /myroute (i.e. without the second segment) will now 404. If this is unwanted – or if you want to define a default value for your asterisk variable – you can make the asterisk token segment optional. This is done by wrapping the segment variable in parentheses and appending a question mark at the end (kudos to @carlcs for this trick, originally given as an answer to my question here:
'myroute(/(?P[-\w]+))?' => array('action' => 'myPlugin/myAction')

With the latter regex, your route will now also match the following:
/myroute (asterisk token will be undefined)
...and in your controller, here's how you could define a default value for the asterisk token variable:
$asteriskToken = isset( $variables[ 'asteriskToken' ] ) ? $variables[ 'asteriskToken' ] : 'myDefaultAsteriskValue';

> **Comments:**
> **username_0**: Worked like a charm. Thanks for both answering it and also answering what I probably would've run into afterwards. You're the man!  
> **username_1**: Happy to be able to help out, @ZackTaylor :)  
> **username_0**: So I did run into one issue, which is when the route has special characters in it (even when running through url_encode() first.) For example, route 'myroute/check-this-out' works fine, but if it's 'myroute/check-this-out%21', it leads to a 404. When using the "asterisk token" in CP, this is not an issue and does not lead to a 404. Any ideas how to get around this?  
> **username_1**: Hi @ZackTaylor, I notice you've created a new thread for the question in your above comment. Here's the link, as a followup: http://craftcms.stackexchange.com/questions/8754/dynamic-routing-via-plugin-with-special-characters/8760#8760  

---

