<p>Yes it’s possible. First you will need to update your route key so that you’re capturing the <code>name</code> parameter with a valid <a href="http://php.net/manual/en/regexp.reference.subpatterns.php" rel="nofollow">named subpattern</a>. (You were close, but missing the actual <em>pattern</em> part of it).</p>

<pre><code>return array(
    'api/test/(?P&lt;name&gt;[^\/]+)' =&gt; ['action' =&gt; 'api/element/section'],
);
</code></pre>

<p>Any named subpattern matches in your route will be passed to your controller action’s <code>$variables</code> argument, if it has one.</p>

<pre><code>class Api_ElementController extends BaseController
{
    // ...

    public function actionSection(array $variables = array())
    {
        $name = $variables['name'];

        // ...
    }
}
</code></pre>

<p>Note that you specifically need to call the argument “<code>$variables</code>”, and you need to typeset it to an <code>array</code> for this to work.</p>

CC BY-SA 3.0
2014-07-11T20:24:11.173Z
Does it look to you like I'm doing something wrong with the regex to get the name? Right now I'm not getting to the actionSection method. I put a commented out version of the original in my question.
Tyler Willingham
CC BY-SA 3.0
2014-07-11T20:32:44.357Z
I’m not sure what that means - is the code above your route or not?
Brandon Kelly
CC BY-SA 3.0
2014-07-11T20:38:12.523Z
Before implementing your change, hitting /actions/api/element/section/name/triviaQuestions/ will trigger actionSection($name) with $name='triviaQuestions'.   However if I use the above route (/api/test/triviaQuestions) I get a 404 - leading me to believe I've done something wrong in my routing.
Tyler Willingham
CC BY-SA 3.0
2014-07-11T21:18:06.587Z
@TylerWillingham Ah, there is a bug in your route. Just updated my answer.
Brandon Kelly
CC BY-SA 3.0
2014-07-11T19:59:15.373Z
2014-07-11T21:17:56.210Z
2014-07-11T21:17:56.210Z
Brandon Kelly
<p>We want to mask specific action routes in our project's routes.php file. Currently what we have looks like this:</p>

<pre><code>return array(
    // /actions/api/element/section/name/{$name}/
    'api/test/(?P&lt;name&gt;)'  =&gt; ['action' =&gt; 'api/element/section'],
);
</code></pre>

<p>We would like to pass name into the action array somehow. Is there something documented or undocumented that I'm missing? Or is what I'm attempting not possible at this time?</p>

<plugin-development><routes><routing>
How can I implement advanced action routes (in routes.php)?
# How can I implement advanced action routes (in routes.php)?

(Asked by: username_0 on 2014-07-11)

We want to mask specific action routes in our project's routes.php file. Currently what we have looks like this:
return array(
    // /actions/api/element/section/name/{$name}/
    'api/test/(?P)'  => ['action' => 'api/element/section'],
);

We would like to pass name into the action array somehow. Is there something documented or undocumented that I'm missing? Or is what I'm attempting not possible at this time?

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


---

## 1 answers

### Answer by username_1 on 2014-07-11. Score: 5 | Accepted answer

Yes it’s possible. First you will need to update your route key so that you’re capturing the name parameter with a valid named subpattern. (You were close, but missing the actual pattern part of it).
return array(
    'api/test/(?P[^\/]+)' => ['action' => 'api/element/section'],
);

Any named subpattern matches in your route will be passed to your controller action’s $variables argument, if it has one.
class Api_ElementController extends BaseController
{
    // ...

    public function actionSection(array $variables = array())
    {
        $name = $variables['name'];

        // ...
    }
}

Note that you specifically need to call the argument “$variables”, and you need to typeset it to an array for this to work.

> **Comments:**
> **username_0**: Does it look to you like I'm doing something wrong with the regex to get the name? Right now I'm not getting to the actionSection method. I put a commented out version of the original in my question.  
> **username_1**: I’m not sure what that means - is the code above your route or not?  
> **username_0**: Before implementing your change, hitting /actions/api/element/section/name/triviaQuestions/ will trigger actionSection($name) with $name='triviaQuestions'.   However if I use the above route (/api/test/triviaQuestions) I get a 404 - leading me to believe I've done something wrong in my routing.  
> **username_1**: @TylerWillingham Ah, there is a bug in your route. Just updated my answer.  

---

