Plugin Routes: Asterisk Token effect from CP Routes 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' =&gt; array('action' =&gt; 'myPlugin/myAction')
  );    
}


Though when I try the asterisk trick, it doesn't work:

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


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

Thanks!
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&lt;asteriskToken&gt;[-\w]+)' =&gt; array('action' =&gt; '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&lt;asteriskToken&gt;[-\w]+))?' =&gt; array('action' =&gt; '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';