<p>You can get the request’s matched element via:</p>



<pre class="lang-php prettyprint-override"><code>$element = craft()-&gt;urlManager-&gt;getMatchedElement();
</code></pre>

<p>There are two things to remember though:</p>

<ol>
<li>There may not be a matched element</li>
<li>The matched element might not be an <em>entry</em>. (It could also be a category, and if you have any plugins installed that add their own element types that can have their own URLs, it could be one of those.)</li>
</ol>

<p>To defend against those possibilities, just use the following conditional:</p>

<pre class="lang-php prettyprint-override"><code>if ($element &amp;&amp; $element-&gt;getElementType() == ElementType::Entry)
{
    // There was a matched element, and it's an entry!
    // ...
}
</code></pre>

CC BY-SA 3.0
2016-07-20T21:07:41.697Z
Will this work in elementapi when setting up end points?
joomkit
CC BY-SA 3.0
2016-07-24T13:54:07.340Z
@joomkit No, in that case the matched element(s) are passed directly to your tranformer function, so there's no need to have a separate way to access them. If you're having trouble trying to get something set up please post as a new question :)
Brandon Kelly
CC BY-SA 3.0
2014-08-08T12:32:35.140Z
2014-08-08T12:39:09.380Z
2014-08-08T12:39:09.380Z
Brandon Kelly
<p>If I’m accessing an entry’s URL (the 4th check in <a href="http://buildwithcraft.com/docs/routing" rel="nofollow">Routing</a>), how can I get the entry that the URL matched from a plugin?</p>

<plugin-development><entrymodel><routing><entry>
How can I get the requested entry from a plugin?
# How can I get the requested entry from a plugin?

(Asked by: username_0 on 2014-08-08)

If I’m accessing an entry’s URL (the 4th check in [Routing](http://buildwithcraft.com/docs/routing)), how can I get the entry that the URL matched from a plugin?

**Tags:** plugin-development, entrymodel, routing, entry

---

## 1 answers

### Answer by username_0 on 2014-08-08. Score: 3 | Accepted answer

You can get the request’s matched element via:
$element = craft()->urlManager->getMatchedElement();

There are two things to remember though:

There may not be a matched element
The matched element might not be an entry. (It could also be a category, and if you have any plugins installed that add their own element types that can have their own URLs, it could be one of those.)

To defend against those possibilities, just use the following conditional:
if ($element && $element->getElementType() == ElementType::Entry)
{
    // There was a matched element, and it's an entry!
    // ...
}

> **Comments:**
> **username_1**: Will this work in elementapi when setting up end points?  
> **username_0**: @joomkit No, in that case the matched element(s) are passed directly to your tranformer function, so there's no need to have a separate way to access them. If you're having trouble trying to get something set up please post as a new question :)  

---

