<p>You can grab the current entry using the <code>$uri = craft()-&gt;request-&gt;getRequestUri()</code> which will return the URI in a format like so: </p>

<p><code>/path/to/uri</code></p>

<p>To grab the entry, its easiest to grab the element first, based on the URI, using <code>craft()-&gt;elements-&gt;getElementByUri($uri)</code>. However, this will not work because of the beginning slash that is returned by <code>getRequestUri</code>. So you must remove the slash using <code>ltrim</code> like so: </p>

<p><code>$uri = ltrim(craft()-&gt;request-&gt;getRequestUri(), '/')</code></p>

<p>Once you have the URI, minus the beginning slash (<code>path/to/uri</code>), you can use <code>$element = craft()-&gt;elements-&gt;getElementByUri($uri)</code> to return the element and then grab the entry using <code>$entry = craft()-&gt;entries-&gt;getEntryById($element-&gt;id)</code>.</p>

<p>If you were going to need this often, you can place it in a helper method on the controller.</p>

<pre><code>private function getEntryByRequestUri()
{
    // remove the first trailing slash
    $uri = ltrim(craft()-&gt;request-&gt;getRequestUri(), '/');
    $element = craft()-&gt;elements-&gt;getElementByUri($uri);
    $entry = craft()-&gt;entries-&gt;getEntryById($element-&gt;id);
    if ($entry == null) {
        // log something and do something
    }
    return $entry;
}
</code></pre>

CC BY-SA 3.0
2016-08-04T16:12:16.127Z
2016-08-04T16:12:16.127Z
Jason McCallister
<p>I'm working a plugin and part of the task is to grab the entry based on the current URI using <code>craft()-&gt;request-&gt;getRequestUri()</code>.</p>

<p>Is there a way to grab the entry based on the URI from the request?</p>

<plugin-development><entries><request>
Retrieving an entry based on the request URI in a plugin controller
# Retrieving an entry based on the request URI in a plugin controller

(Asked by: username_0 on 2016-08-04)

I'm working a plugin and part of the task is to grab the entry based on the current URI using `craft()->request->getRequestUri()`.

Is there a way to grab the entry based on the URI from the request?

**Tags:** plugin-development, entries, request


---

## 1 answers

### Answer by username_0 on 2016-08-04. Score: 2 | Accepted answer

You can grab the current entry using the $uri = craft()->request->getRequestUri() which will return the URI in a format like so: 
/path/to/uri
To grab the entry, its easiest to grab the element first, based on the URI, using craft()->elements->getElementByUri($uri). However, this will not work because of the beginning slash that is returned by getRequestUri. So you must remove the slash using ltrim like so: 
$uri = ltrim(craft()->request->getRequestUri(), '/')
Once you have the URI, minus the beginning slash (path/to/uri), you can use $element = craft()->elements->getElementByUri($uri) to return the element and then grab the entry using $entry = craft()->entries->getEntryById($element->id).
If you were going to need this often, you can place it in a helper method on the controller.
private function getEntryByRequestUri()
{
    // remove the first trailing slash
    $uri = ltrim(craft()->request->getRequestUri(), '/');
    $element = craft()->elements->getElementByUri($uri);
    $entry = craft()->entries->getEntryById($element->id);
    if ($entry == null) {
        // log something and do something
    }
    return $entry;
}


---

