Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-20T21:07:41.697Z
Answers.Text: Will this work in elementapi when setting up end points?
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-24T13:54:07.340Z
Answers.Text: @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 :)
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-08T12:32:35.140Z
Answers.LastActivityDate: 2014-08-08T12:39:09.380Z
Answers.LastEditDate: 2014-08-08T12:39:09.380Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Tags: <plugin-development><entrymodel><routing><entry>
Title: How can I get the requested entry from a plugin?
ThreadText: # 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:
  
  ```php
  $element = craft()->urlManager->getMatchedElement();
  
  ```
  
  There are two things to remember though:
  
  1. There may not be a matched element
  2. 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:
  
  ```php
  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**: @username_1 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 :)  
  
  ---
  
  