<p>The <a href="https://github.com/craftcms/cms/blob/develop/src/web/UrlManager.php#L378-L413" rel="nofollow noreferrer"><code>UrlManager::_getMatchedElementRoute()</code></a> method does it something like this:</p>
<pre class="lang-php prettyprint-override"><code>$path = Craft::$app-&gt;getRequest()-&gt;getPathInfo();
$sitedId = Craft::$app-&gt;getSites()-&gt;getCurrentSite()-&gt;id;

if ($path !== Element::HOMEPAGE_URI) {
    $element = Craft::$app-&gt;getElements()-&gt;getElementByUri($path, $siteId, true);
}
</code></pre>
<p>The logic for determining the element can be found in <a href="https://github.com/craftcms/cms/blob/d7874eb87269cf995ed5a72a7ccb5c0851c41b7e/src/services/Elements.php#L838-L886" rel="nofollow noreferrer"><code>Elements::getElementByUri</code></a>.</p>
<p>I actually cover some of this in the <a href="https://putyourlightson.com/reading-crafts-source-code#bootstrapping-the-craft-application" rel="nofollow noreferrer">Bootstrapping the Craft Application</a> video of the free <a href="https://putyourlightson.com/reading-crafts-source-code" rel="nofollow noreferrer">Reading Craft's Source Code</a> series, which in my biased opinion is well worth a watch.</p>

CC BY-SA 4.0
2023-01-09T10:46:08.870Z
Thanks Ben! I already watched part of your series, but somehow lost track of it.
Matthias Redl-Mann
CC BY-SA 4.0
2023-01-02T16:20:18.220Z
2023-01-02T16:27:49.470Z
2023-01-02T16:27:49.470Z
Ben Croker
<p>From PHP (Plugin), how can I load a certain entry if only the full URL is provided?
I can not find Craft's logic in PHP to split the full URL into the site's baseUrl and the entry's URI.</p>
<p>Please note it's a multisite setup, so my site's baseUrl might look like '<code>@web/whatever</code>'</p>
<p>Thanks</p>

<plugin-development><entry><services><entry-query>
Entry Query: How to load an entry with the full URL as only criterion
# Entry Query: How to load an entry with the full URL as only criterion

(Asked by: username_0 on 2022-12-19)

From PHP (Plugin), how can I load a certain entry if only the full URL is provided?
I can not find Craft's logic in PHP to split the full URL into the site's baseUrl and the entry's URI.

Please note it's a multisite setup, so my site's baseUrl might look like '`@web/whatever`'

Thanks

**Tags:** plugin-development, entry, services, entry-query


---

## 1 answers

### Answer by username_1 on 2023-01-02. Score: 2 | Accepted answer

The UrlManager::_getMatchedElementRoute() method does it something like this:
$path = Craft::$app->getRequest()->getPathInfo();
$sitedId = Craft::$app->getSites()->getCurrentSite()->id;

if ($path !== Element::HOMEPAGE_URI) {
    $element = Craft::$app->getElements()->getElementByUri($path, $siteId, true);
}

The logic for determining the element can be found in Elements::getElementByUri.
I actually cover some of this in the Bootstrapping the Craft Application video of the free Reading Craft's Source Code series, which in my biased opinion is well worth a watch.

> **Comments:**
> **username_0**: Thanks Ben! I already watched part of your series, but somehow lost track of it.  

---

