Site Repository

To work with the with Site Repository, use the following Facade:

use Statamic\Facades\Site;

Methods

Methods Description
all() Get all Sites
get($handle) Get Site by handle
findByUrl($url) Get site by a given URL
current() Returns the current site, determined by the current page URL.
setCurrent($handle) Sets the current site.
selected() Returns the site currently selected in the Control Panel
setSelected($handle) Sets the selected site.
default() Returns the "default site", which will be the first site in the sites config
authorized() Returns a collection of Site objects, where the user is authorized to view the site (see Multisite Permissions).
hasMultiple() Returns a boolean indicating if multiple sites are configured.
setConfig($config) Accepts an array. Allows you to override the configured sites.

Resolving the current site

When you're using the Site::current() method, sometimes the current page URL will lead to the wrong site being returned (like when using Livewire). In this case, you can use the resolveCurrentUrlUsing to check against a different URL:

// app/Providers/AppServiceProvider.php
 
use Statamic\Facades\Site;
 
Site::resolveCurrentUrlUsing(fn () => Livewire::originalUrl());
HR: Section
Learn More!

There is more to learn more in these related articles:

Docs

HR: Section
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →