Answers.Body: <p>I know this is a year and a half late, but maybe it helps someone else. I have a large multi-site project with a site for country and language. For example:</p>
  
  <ul>
  <li>Germany (German) - <strong>default</strong> - <code>/de-de/</code></li>
  <li>Germany (English) - <code>/en-de/</code></li>
  <li>Austria (German) - <code>/de-at/</code></li>
  <li>Austria (English) - <code>/en-at/</code></li>
  <li>etc</li>
  </ul>
  
  <p>I was having this problem when posting to one of my controller's actions and then trying to get the <code>currentSite</code> within my plugin via <code>Craft::$app-&gt;getSites()-&gt;currentSite</code>.</p>
  
  <p>In my JavaScript, my action looked like:</p>
  
  <pre><code>/actions/pluginHandle/controllerClass/actionMethod
  </code></pre>
  
  <p>Whenever I'd fetch the current site with <code>Craft::$app-&gt;getSites()-&gt;currentSite</code> it would always return the default site (Germany/German), and that's because I wasn't including the first URL segment (to get Austria/English, for example):</p>
  
  <pre><code>/en-at/actions/pluginHandle/controllerClass/actionMethod
  </code></pre>
  
  <p>Without that, Craft was resorting to getting the default site.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-17T16:37:06.607Z
Answers.LastActivityDate: 2020-03-17T16:37:06.607Z
Answers.UserDisplayName: Ryan
Answers.Body: <p>With Craft CMS 3, you can spit out current site values directly with <code>currentSite</code>.</p>
  
  <pre><code>Site ID:            {{ currentSite.id }}
  Site Handle:        {{ currentSite.handle }}
  Site Name:          {{ currentSite.name }}
  Site Language:      {{ currentSite.language }}
  Is Primary Site?:   {{ currentSite.primary }}
  Base URL:           {{ currentSite.baseUrl }}
  </code></pre>
  
  <p><a href="https://docs.craftcms.com/v3/sites.html#site-groups" rel="nofollow noreferrer">https://docs.craftcms.com/v3/sites.html#site-groups</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-07T06:00:32.183Z
Answers.Text: Question was about controllers; not twig.
Answers.UserDisplayName: biko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-17T23:50:41.023Z
Answers.LastActivityDate: 2020-03-17T23:50:41.023Z
Answers.UserDisplayName: Tammy Shipps
Body: <p>When my multisite is set up as @web/site, I cannot access the current site handle from my controllers.</p>
  
  <pre><code>Craft::$app-&gt;getSites()-&gt;currentSite-&gt;handle
  </code></pre>
  
  <p>I think this is because my form action defaults to the root site ie @web/actions/controller... rather than @web/site/actions/controller...</p>
  
  <p>Is there a way to get the current site or do I need to pass it into the controller from the form?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-08-29T23:47:14.077Z
Comments.Text: Why doesn't `Craft::$app->getSites()->currentSite->handle` work? What is it returning for you?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-10T01:12:21.543Z
Comments.Text: It is returning the default site handle. I have a domain name example.com and multisites set up as example.com/site1 and example.com/site2. The controller gets called from example.com/actions/controller therefor retuning example as the site handle and not site1 or site2.
Comments.UserDisplayName: David A McInnis
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-17T14:25:03.567Z
Comments.Text: I'm having the same problem. Did you ever figure this out?
Comments.UserDisplayName: Ryan
Tags: <plugin-development><craft3>
Title: How do I access current site handle in Craft 3 controllers
ThreadText: # How do I access current site handle in Craft 3 controllers
  
  (Asked by: username_0 on 2018-08-29)
  
  When my multisite is set up as @web/site, I cannot access the current site handle from my controllers.
  
  ```
  Craft::$app->getSites()->currentSite->handle
  
  ```
  
  I think this is because my form action defaults to the root site ie @web/actions/controller... rather than @web/site/actions/controller...
  
  Is there a way to get the current site or do I need to pass it into the controller from the form?
  
  **Tags:** plugin-development, craft3
  
  > **Comments:**
  > **username_1**: Why doesn't `Craft::$app->getSites()->currentSite->handle` work? What is it returning for you?  
  > **username_0**: It is returning the default site handle. I have a domain name example.com and multisites set up as example.com/site1 and example.com/site2. The controller gets called from example.com/actions/controller therefor retuning example as the site handle and not site1 or site2.  
  > **username_2**: I'm having the same problem. Did you ever figure this out?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-03-17. Score: 6
  
  I know this is a year and a half late, but maybe it helps someone else. I have a large multi-site project with a site for country and language. For example:
  
  * Germany (German) - **default** - `/de-de/`
  * Germany (English) - `/en-de/`
  * Austria (German) - `/de-at/`
  * Austria (English) - `/en-at/`
  * etc
  
  I was having this problem when posting to one of my controller's actions and then trying to get the `currentSite` within my plugin via `Craft::$app->getSites()->currentSite`.
  
  In my JavaScript, my action looked like:
  
  ```
  /actions/pluginHandle/controllerClass/actionMethod
  
  ```
  
  Whenever I'd fetch the current site with `Craft::$app->getSites()->currentSite` it would always return the default site (Germany/German), and that's because I wasn't including the first URL segment (to get Austria/English, for example):
  
  ```
  /en-at/actions/pluginHandle/controllerClass/actionMethod
  
  ```
  
  Without that, Craft was resorting to getting the default site.
  
  
  ---
  
  ### Answer by username_3 on 2020-03-17. Score: 2
  
  With Craft CMS 3, you can spit out current site values directly with `currentSite`.
  
  ```
  Site ID:            {{ currentSite.id }}
  Site Handle:        {{ currentSite.handle }}
  Site Name:          {{ currentSite.name }}
  Site Language:      {{ currentSite.language }}
  Is Primary Site?:   {{ currentSite.primary }}
  Base URL:           {{ currentSite.baseUrl }}
  
  ```
  
  <https://docs.craftcms.com/v3/sites.html#site-groups>
  
  > **Comments:**
  > **username_4**: Question was about controllers; not twig.  
  
  ---
  
  