0

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!

2 Answers 2

2

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.

1
  • This is helpful, thank you!
    – supazu
    Oct 19, 2022 at 4:47
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
2
  • Hmm so my route works like this 'playing' => 'playing/playing/tag', but I now get a 403 error and trying to workout why.
    – supazu
    Oct 16, 2022 at 5:42
  • 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
    Oct 16, 2022 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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