<p>When searching the Craft 3.5.x docs for <code>siteName</code>, I see references to <code>Global Variables</code> and <code>General Config Settings</code>. Screenshot: <a href="https://cln.sh/egvIXhxZSzaFjeCgNLxX" rel="nofollow noreferrer">https://cln.sh/egvIXhxZSzaFjeCgNLxX</a></p>
<p>However, AFAICT, it works differently for modules/plugins. As per the Sites section in the Craft 3.5.x Class Reference docs at <a href="https://docs.craftcms.com/api/v3/craft-services-sites.html" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-services-sites.html</a>, you would do something like one of the following:</p>
<pre class="lang-php prettyprint-override"><code>Craft::$app-&gt;sites-&gt;currentSite-&gt;name;
Craft::$app-&gt;sites-&gt;primarySite-&gt;name;
Craft::$app-&gt;sites-&gt;getSiteByHandle('default')-&gt;name;
Craft::$app-&gt;sites-&gt;getSiteById(1)-&gt;name;
</code></pre>
<p>I'm getting the <code>name</code> part because I'm using PhpStorm with autocomplete set up as per article at <a href="https://nystudio107.com/blog/auto-complete-craft-cms-3-apis-in-twig-with-phpstorm" rel="nofollow noreferrer">https://nystudio107.com/blog/auto-complete-craft-cms-3-apis-in-twig-with-phpstorm</a></p>
<p>Screenshot of autocomplete in action: <a href="https://cln.sh/T77W1Y69SmR3qPUPv0sb" rel="nofollow noreferrer">https://cln.sh/T77W1Y69SmR3qPUPv0sb</a></p>
<p>I'm successfully using <code>Craft::$app-&gt;sites-&gt;getSiteById(1)-&gt;name;</code> in one of my custom modules.</p>

CC BY-SA 4.0
2020-12-06T19:28:13.720Z
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!!
Cat Named Dog
CC BY-SA 4.0
2020-12-06T21:48:22.323Z
In support of this answer, the `siteName` Twig global is just a shortcut to the current site's `->name`. See https://github.com/craftcms/cms/blob/de7fbb5043e4d5da4b3fb5030c3882188e201c0e/src/web/twig/Extension.php#L1312
Evan Warner
CC BY-SA 4.0
2020-12-07T07:48:20.047Z
And in more support of this answer, `siteName` and `siteUrl` have been [deprecated from 3.6](https://github.com/craftcms/cms/blob/3.6/CHANGELOG.md#deprecated) :)
Oli
CC BY-SA 4.0
2020-12-07T14:38:59.733Z
@Oli So I SHOULD actually just use the `Craft::$app->config->general->siteName` and not what setting is in the CP?
Cat Named Dog
CC BY-SA 4.0
2020-12-07T21:56:48.367Z
@good_afternoon: You can see [here](https://github.com/craftcms/cms/blob/de7fbb5043e4d5da4b3fb5030c3882188e201c0e/src/web/twig/Extension.php#L1312) 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.
RitterKnight
CC BY-SA 4.0
2020-12-07T22:08:53.590Z
The thing [deprecated](https://github.com/craftcms/cms/blob/3.6/src/config/GeneralConfig.php#L1233) is setting ``siteUrl`` and ``siteName`` from [``config/general.php``](https://craftcms.com/docs/3.x/config/config-settings.html) so you should avoid calling the config class where it gets the data from.
RitterKnight
CC BY-SA 4.0
2020-12-06T19:22:28.287Z
2020-12-07T14:44:12.500Z
2020-12-07T14:44:12.500Z
Andrea DeMers
<p>In templates I can do something like <code>{{siteName}}</code>. But how do I retrieve this in a module?</p>
<p>I've tried:</p>
<p><code>Craft::$app-&gt;config-&gt;siteName</code></p>
<p>I can grab my custom globals via <code>Craft::$app-&gt;globals</code>, but the class reference doesn't say anything about the defaults like <code>siteName</code></p>

<craft3><modules><globals>
How do I get the siteName in a module (Craft 3)
# How do I get the siteName in a module (Craft 3)

(Asked by: username_0 on 2020-12-06)

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

I've tried:

`Craft::$app->config->siteName`

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

**Tags:** craft3, modules, globals

---

## 1 answers

### Answer by username_1 on 2020-12-06. Score: 4 | Accepted answer

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:
Craft::$app->sites->currentSite->name;
Craft::$app->sites->primarySite->name;
Craft::$app->sites->getSiteByHandle('default')->name;
Craft::$app->sites->getSiteById(1)->name;

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.

> **Comments:**
> **username_0**: 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!!  
> **username_2**: In support of this answer, the `siteName` Twig global is just a shortcut to the current site's `->name`. See https://github.com/craftcms/cms/blob/de7fbb5043e4d5da4b3fb5030c3882188e201c0e/src/web/twig/Extension.php#L1312  
> **username_3**: And in more support of this answer, `siteName` and `siteUrl` have been [deprecated from 3.6](https://github.com/craftcms/cms/blob/3.6/CHANGELOG.md#deprecated) :)  
> **username_0**: @Oli So I SHOULD actually just use the `Craft::$app->config->general->siteName` and not what setting is in the CP?  
> **username_4**: @good_afternoon: You can see [here](https://github.com/craftcms/cms/blob/de7fbb5043e4d5da4b3fb5030c3882188e201c0e/src/web/twig/Extension.php#L1312) 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.  
> **username_4**: The thing [deprecated](https://github.com/craftcms/cms/blob/3.6/src/config/GeneralConfig.php#L1233) is setting ``siteUrl`` and ``siteName`` from [``config/general.php``](https://craftcms.com/docs/3.x/config/config-settings.html) so you should avoid calling the config class where it gets the data from.  

---

