Route to module controller action not working for me?

New to Yii.

Created my first module. Generated a crud controller with gii. All good.

Then I add my own action. Got an icon and link in the action column, good. But the route goes to 404.


namespace app\modules\mbh\controllers\foxvid;

...

class FoxvidController extends Controller

{

    public function actionFoo($id)

    {

        die('foo');

    }

These URLs work without any effort from me:

index.php?r=mbh/foxvid/index

index.php?r=mbh/foxvid/update&id=4

index.php?r=mbh/foxvid/view&id=4

I expected index.php?r=mbh/foxvid/foo&id=4 to be routed same as index, delete, view etc.

Works for SiteController (just by coding actionFoo() ) but not for module controller at /modules/foxvid/FoxvidController

Searching is turning up a lot of v1 answers, and a lot about regular expressions in a config (say which config guys!!).

But I do not think I should be coding anything more? I do not think I need to specify routes if they are actions in a controller?

Unclear why switching to a module made non-standard routes fail. Where do I need to look for more info?

The routes are either:


controller/action



or


module/controller/action

Yeah it seems simple. Which makes this stupid :(

module=mbh

controller=foxvid

action=index

works great

module=mbh

controller=foxvid

action=foo

is a 404. I’m not reaching the controller.

Don’t know enough yet to recognize where this can come from.

/vendor/yiisoft/yii2/base/Controller.php method runAction() gets $id=foo but immediately createAction() returns NULL.

The saga continues tomorrow, or I’ll stop attempting a module this earlyii in the gamee ;)

Something is baked in somewhere…

How do you create the url for ‘foo’?

I just type it in the address bar ;)

Here is my NavWidget config from layouts/main.php . Index works, Foo does not.


            

        ['label' => 'Videos', 'url' => ['/mbh/foxvid/index']],

        ['label' => 'Foo', 'url' => ['/mbh/foxvid/foo']],



If I have debugged correctly, the "method exists" call is failing.

DUMP output for a working call looks the same to me.

I am also getting a clue from the phpStorm IDE, but it is going over my head.

Notice the $id argument to Foo is greyed.

But the working actions (create, update,view),…) are not.

So phpStorm sees something …? http://…YIIMODULE.png

[b]

/vendor/yiisoft/yii2/base/Controller.php[/b] Here I dumped some vars to narrow it down to the method_exists call…


    public function createAction($id)

    {

        if ($id === '') {

            $id = $this->defaultAction;

        }


        $actionMap = $this->actions();

\stewlog::dump('ACTION MAP', $actionMap);

        if (isset($actionMap[$id])) {

            return Yii::createObject($actionMap[$id], [$id, $this]);

        } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) {

            $methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));

\stewlog::dump('EXISTS?', get_class($this) . '::'. $methodName,  (method_exists($this, $methodName) ?'YES':'NO' ));

            if (method_exists($this, $methodName)) {

                $method = new \ReflectionMethod($this, $methodName);

                if ($method->isPublic() && $method->getName() === $methodName) {

                    return new InlineAction($id, $this, $methodName);

                }

            }

        }

        \stewlog::bm('nullret');

DUMP: Here the method_exists call fails


-------------------

2016-04-08 06:48:53

string(13) "runAction id="


string(3) "foo"


string(7) "params="


array(1) {

  'r' =>

  string(14) "mbh/foxvid/foo"

}




-------------------

2016-04-08 06:48:53

string(10) "ACTION MAP"


array(0) {

}




-------------------

2016-04-08 06:48:53

string(7) "EXISTS?"


string(55) "app\modules\mbh\controllers\FoxvidController::actionFoo"


string(2) "NO"




-------------------

2016-04-08 06:48:53

string(7) "nullret"



[b]

/modules/mbh/controllers/foxvid/FoxvidController.php[/b] Here I am namespaced and public, other calls wrk, why not Foo? Durn it.


namespace app\modules\mbh\controllers\foxvid;

...

class FoxvidController extends Controller

{

...

    public function actionFoo($id)

    {

        die('foo');

    }

}

Two things:

[list=1]

[*]$id is greyed out because the IDE tells you, this parameter is not used in the method.

[*]don’t use die in your action. it likely prevents writing to your application log. Yii writes to your log targets when the request is finished. “exit” and “die” prevents this

[/list]

Oh and your namespace is wrong:

namespace app\modules\mbh\controllers\foxvid;

as opposed to

"app\modules\mbh\controllers\FoxvidController::actionFoo"

That’s probably a bingo, thank you. As I have not been using namespaces before, that is where I’d expect the issue.

Pulled it apart and did it another way, so cannot verify your fix immediately. Clearly I have more to learn about namespacing and Yii, so this is not over for me.

But by doing it differently (and solving those issues!), and with your eye on my bad namespace, I begin to get the picture.

Thank you very much.