<p>You'll need to update the regex to allow for additional characters.  For example, if you want to allow <code>!</code>, then this will work:</p>

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

<p>Note that <code>\w</code> is essentially the same as writing <code>[A-Za-z0-9_]</code>.</p>

CC BY-SA 3.0
2015-03-19T23:03:42.493Z
But this does work when manually adding the route in CP using the aterisk, as long as the url/route uses url_encode. Characters like "!" aren't being passed in the route, the url_encode() character is (such as %21) There must be a way to do '(?P<myVariable>[-\w]+)' in order to get the same functionality as the CP asterisk token no?
taylor
CC BY-SA 3.0
2015-03-19T23:18:40.797Z
You're right, I'm an idiot. Updating answer.
Brad Bell
CC BY-SA 3.0
2015-03-19T23:46:36.673Z
Nifty trick, super good to know. Works perfectly!
taylor
CC BY-SA 3.0
2015-03-19T22:20:47.907Z
2015-03-19T23:22:10.330Z
2015-03-19T23:22:10.330Z
Brad Bell
<p>After learning how to use dynamic routing in MyPluginPlugin.php, I quickly ran into the issue of if the URL has special characters from url_encode:</p>

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

<p>And then I process some variables before rendering the template in myPlugin/myAction:</p>

<pre><code> public function actionLoadTemplate( array $variables = array() )
 {
   /*
   process &amp; build $variables
   */

    $this-&gt;renderTemplate('layouts/forums/_entry', $variables);

 }
</code></pre>

<p>This works great if the route looks like <code>myroute/check-this-out</code>, however if the route had a special character in it, say it was "check-this-out!", it understandably leads to a 404. But with using url_encode on the URL so it looks like <code>myroute/check-this-out%21</code>, it still leads to a 404. </p>

<p>When testing manually in the CP route section, if I create <code>myroute/*</code> with the token, I don't get a 404 with those special characters after using url_encode.</p>

<p>Is there a way to modify <code>(?P&lt;myVariable&gt;[-\w]+)</code> so that it works with url-safe special characters just like the CP version does? </p>

<plugin-development><routes><php>
Dynamic routing via plugin with special characters
# Dynamic routing via plugin with special characters

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

After learning how to use dynamic routing in MyPluginPlugin.php, I quickly ran into the issue of if the URL has special characters from url_encode:
public function registerSiteRoutes()
{
return array(
    'myroute/(?P[-\w]+)' => array('action' => 'myPlugin/myAction')
);    
}

And then I process some variables before rendering the template in myPlugin/myAction:
 public function actionLoadTemplate( array $variables = array() )
 {
   /*
   process & build $variables
   */

    $this->renderTemplate('layouts/forums/_entry', $variables);

 }

This works great if the route looks like myroute/check-this-out, however if the route had a special character in it, say it was "check-this-out!", it understandably leads to a 404. But with using url_encode on the URL so it looks like myroute/check-this-out%21, it still leads to a 404. 
When testing manually in the CP route section, if I create myroute/* with the token, I don't get a 404 with those special characters after using url_encode.
Is there a way to modify (?P[-\w]+) so that it works with url-safe special characters just like the CP version does?

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


---

## 1 answers

### Answer by username_1 on 2015-03-19. Score: 4 | Accepted answer

You'll need to update the regex to allow for additional characters.  For example, if you want to allow !, then this will work:
return array(
    'myroute/(?P[-\w\!]+)' => array('action' => 'myPlugin/myAction')
); 

Note that \w is essentially the same as writing [A-Za-z0-9_].

> **Comments:**
> **username_0**: But this does work when manually adding the route in CP using the aterisk, as long as the url/route uses url_encode. Characters like "!" aren't being passed in the route, the url_encode() character is (such as %21) There must be a way to do '(?P<myVariable>[-\w]+)' in order to get the same functionality as the CP asterisk token no?  
> **username_1**: You're right, I'm an idiot. Updating answer.  
> **username_0**: Nifty trick, super good to know. Works perfectly!  

---

