In templates I can do something like {{siteName}}. But how do I retrieve this in a module?

I've tried:


I can grab my custom globals via Craft::$app->globals, but the class reference doesn't say anything about the defaults like siteName


When searching the Craft 3.5.x docs for siteName, I see references to Global Variables and General Config Settings. Screenshot: https://cln.sh/egvIXhxZSzaFjeCgNLxX

However, AFAICT, it works differently for modules/plugins. As per the Sites section in the Craft 3.5.x Class Reference docs at https://docs.craftcms.com/api/v3/craft-services-sites.html, you would do something like one of the following:


I'm getting the name part because I'm using PhpStorm with autocomplete set up as per article at https://nystudio107.com/blog/auto-complete-craft-cms-3-apis-in-twig-with-phpstorm

Screenshot of autocomplete in action: https://cln.sh/T77W1Y69SmR3qPUPv0sb

I'm successfully using Craft::$app->sites->getSiteById(1)->name; in one of my custom modules.

  • Much appreciated! I never used multiple sites before, so I didn't even think of having to access it by current site. thank you thank you!! Dec 6 '20 at 19:28
  • 2
    In support of this answer, the siteName Twig global is just a shortcut to the current site's ->name. See github.com/craftcms/cms/blob/… Dec 6 '20 at 21:48
  • And in more support of this answer, siteName and siteUrl have been deprecated from 3.6 :)
    – Oli
    Dec 7 '20 at 7:48
  • @Oli So I SHOULD actually just use the Craft::$app->config->general->siteName and not what setting is in the CP? Dec 7 '20 at 14:38
  • @good_afternoon: You can see here from the source code how the siteName twig variable is being set. As mentioned in the answer, Craft::$app->getSites()->getCurrentSite()->name will be equivalent. Dec 7 '20 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.