<p>I think there is an issue with your namespacing.</p>
<p>In <code>config/app.php</code>:</p>
<pre class="lang-php prettyprint-override"><code>'modules' =&gt; [
    'playing' =&gt; modules\site\Playing::class,
],
'bootstrap' =&gt; ['playing'],
</code></pre>
<p>In <code>modules/site/playing/controllers/PlayingController.php</code></p>
<pre class="lang-php prettyprint-override"><code>namespace modules\site\controllers
</code></pre>
<p>In your <code>composer.json</code></p>
<pre class="lang-json prettyprint-override"><code>&quot;autoload&quot;: {
    &quot;psr-4&quot;: {
        &quot;modules\\site\\&quot;: &quot;modules/site/playing&quot;
    }
},
</code></pre>
<p>Then <code>composer dump-autoload -a</code>.</p>
<p>After this, your <code>https://example.site/actions/playing/playing/tag</code> action should work without a route.</p>

CC BY-SA 4.0
2022-10-19T04:47:33.563Z
This is helpful, thank you!
supazu
CC BY-SA 4.0
2022-10-18T10:46:12.370Z
2022-10-18T10:46:12.370Z
Oli
<p>Typical controller path - <em>actions</em>/<em>handle</em>/<em>controller</em>/<em>action</em></p>
<p>So probably it should be:</p>
<pre><code>'actions/playing/playing/tag =&gt; '/playing',
</code></pre>
<p>or</p>
<pre><code>'actions'/playing/playing/tag =&gt; ['template' =&gt; 'playing'],
</code></pre>
<p>Also, did you tell the composer to add a new class?</p>
<pre><code>composer dump-autoload -a
</code></pre>

CC BY-SA 4.0
2022-10-16T05:42:26.080Z
Hmm so my route works like this `'playing' => 'playing/playing/tag',` but I now get a 403 error and trying to workout why.
supazu
CC BY-SA 4.0
2022-10-16T05:47:49.463Z
Adding `public int|bool|array $allowAnonymous = true;` makes it work but it's got no context of anything it seems; despite being logged in, `Craft::$app->getUser()->getIdentity()` returns null...hence the 403 error I guess.
supazu
CC BY-SA 4.0
2022-10-15T06:11:15.773Z
2022-10-15T06:11:15.773Z
RomanAvr
<p>I'm trying to set up a module with a controller as bit of a learning tool. The learning is not going well so far. Here is my setup:</p>
<pre><code>- modules
- - site
- - - playing
- - - - Playing.php
- - - - controllers
- - - - - PlayingController.php
</code></pre>
<p>Inside <code>config/app.php</code> I have: <code>&quot;playing&quot; =&gt; site\playing\Playing::class</code> and it's being bootstraped.</p>
<pre><code># modules/site/playing/Playing.php

namespace site\playing;

use yii\base\Module;

class Playing extends Module
{

}
</code></pre>
<pre><code># modules/site/playing/controllers/PlayingController.php

namespace site\playing\controllers {

use craft\web\Controller;

class PlayingController extends Controller
  {
    public function actionTag() {
      echo 'This works';
    }
  }
}
</code></pre>
<p>And I have a route <code>'playing' =&gt; 'site/playing/tag'</code>, but when I go to <code>mysite.dev/playing</code> I get a 404.</p>
<p>I'm sure it's just something super simple I'm missing but I'd appreciate any help!</p>

<modules><controller>
Setting up and understanding a controller
# Setting up and understanding a controller

(Asked by: username_0 on 2022-10-15)

I'm trying to set up a module with a controller as bit of a learning tool. The learning is not going well so far. Here is my setup:
- modules
- - site
- - - playing
- - - - Playing.php
- - - - controllers
- - - - - PlayingController.php

Inside config/app.php I have: "playing" => site\playing\Playing::class and it's being bootstraped.
# modules/site/playing/Playing.php

namespace site\playing;

use yii\base\Module;

class Playing extends Module
{

}

# modules/site/playing/controllers/PlayingController.php

namespace site\playing\controllers {

use craft\web\Controller;

class PlayingController extends Controller
  {
    public function actionTag() {
      echo 'This works';
    }
  }
}

And I have a route 'playing' => 'site/playing/tag', but when I go to mysite.dev/playing I get a 404.
I'm sure it's just something super simple I'm missing but I'd appreciate any help!

**Tags:** modules, controller


---

## 2 answers

### Answer by username_1 on 2022-10-18. Score: 2 | Accepted answer

I think there is an issue with your namespacing.
In config/app.php:
'modules' => [
    'playing' => modules\site\Playing::class,
],
'bootstrap' => ['playing'],

In modules/site/playing/controllers/PlayingController.php
namespace modules\site\controllers

In your composer.json
"autoload": {
    "psr-4": {
        "modules\\site\\": "modules/site/playing"
    }
},

Then composer dump-autoload -a.
After this, your https://example.site/actions/playing/playing/tag action should work without a route.

> **Comments:**
> **username_0**: This is helpful, thank you!  

---

### Answer by username_2 on 2022-10-15. Score: 0

Typical controller path - actions/handle/controller/action
So probably it should be:
'actions/playing/playing/tag => '/playing',

or
'actions'/playing/playing/tag => ['template' => 'playing'],

Also, did you tell the composer to add a new class?
composer dump-autoload -a

> **Comments:**
> **username_0**: Hmm so my route works like this `'playing' => 'playing/playing/tag',` but I now get a 403 error and trying to workout why.  
> **username_0**: Adding `public int|bool|array $allowAnonymous = true;` makes it work but it's got no context of anything it seems; despite being logged in, `Craft::$app->getUser()->getIdentity()` returns null...hence the 403 error I guess.  

---

