Module Invalidrouteexception Troubles

Hi All,

I’m scratching my head about how to set up a Yii2 module correctly. It seems my module class is loading, however my default index view is returning a 404 error.

Here are the pieces that I think are important. If anyone has any suggestions or can see what I’m doing incorrectly, your advice is greatly appreciated!

Here, I have my testing module declared in my main.php config file:




    'modules' => [

    	'testmod' => [

    		'class' => 'cics\modules\testmod\Module',

    	],

    ],



Then I created a menu link in my frontend layout, trying to access the index.php file for the test module.




            $menuItems = [

                ['label' => 'Home', 'url' => ['/site/index']],

                ['label' => 'About', 'url' => ['/site/about']],

                ['label' => 'Test Module', 'url' => ['/testmod/index']],

                ['label' => 'Contact', 'url' => ['/site/contact']],

            ];



Then, my module class looks like this:




namespace cics\modules\testmod;


class Module extends \yii\base\Module

{

	public function init()

	{

	    parent::init();

	}

}



My module file structure is identical to what is outlined in the Yii2 docs, however I’ve not set up any models yet, and I’ve not tried to use any layouts, so the models and layouts directories are empty.




yii2-test-mod/

    Module.php                   the module class file

    controllers/                 containing controller class files

        DefaultController.php    the default controller class file

    models/                      containing model class files

    views/                       containing controller view and layout files

        layouts/                 containing layout view files

        default/                 containing view files for DefaultController

            index.php            the index view file



Where my index.php file simply has:




<?php

echo '<h1>This is my index view.</h1>';



My DefaultController.php is pretty basic too:




namespace cics\modules\testmod\controllers;


use yii\web\Controller;

use Yii;


class DefaultController extends Controller

{

    public function actionIndex()

    {

        return $this->render('index');

    }

}



At least how I think I understand things … having set the module in my main.php config file, my routes should be the module_name/controllerID/viewID. Since I’m using the DefaultController, my view file is in the views/default/index.php directory, and my DefaultController has the actionIndex() function that should be rendering the index.php view file.

Here is the error message in the debugger:

8 00:23:03.572 trace yii\base\Module::getModule Loading module: testmod

9 00:23:03.573 error yii\web\HttpException:404

exception ‘yii\base\InvalidRouteException’ with message ‘Unable to resolve the request “testmod/index”.’ in /Users/calebcrosby/Sites/demoyii2advanced/vendor/yiisoft/yii2/base/Module.php:466 Stack trace: #0 /Users/calebcrosby/Sites/demoyii2advanced/vendor/yiisoft/yii2/web/Application.php(82): yii\base\Module->runAction(‘testmod/index’, Array) #1 /Users/calebcrosby/Sites/demoyii2advanced/vendor/yiisoft/yii2/base/Application.php(369): yii\web\Application->handleRequest(Object(yii\web\Request)) #2 /Users/calebcrosby/Sites/demoyii2advanced/frontend/web/index.php(17): yii\base\Application->run() #3 {main} Next exception ‘yii\web\NotFoundHttpException’ with message ‘Unable to resolve the request “testmod/index”.’ in /Users/calebcrosby/Sites/demoyii2advanced/vendor/yiisoft/yii2/web/Application.php:94 Stack trace: #0 /Users/calebcrosby/Sites/demoyii2advanced/vendor/yiisoft/yii2/base/Application.php(369): yii\web\Application->handleRequest(Object(yii\web\Request)) #1 /Users/calebcrosby/Sites/demoyii2advanced/frontend/web/index.php(17): yii\base\Application->run() #2 {main}

Any advice anyone can offer is greatly, greatly appreciated!!

If there is any other information I can provide that will help figure this out, please let me know!

pull up your browser and try this url /testmod/default/index and see if that works. its likely the problem

Thanks so much for your help! Unfortunately, /testmod/default/index returns the same 404 error. In the debug trace I can see that the 404 get triggered right after the testmod module gets loaded. I think it makes sense that /testmod/, /testmod/index, and /testmod/default/index would all return the same view and the same 404 error. But why the view is having the 404 error still alludes me.

I see what you saying I have setup a test app to just reproduce the error. your code works fine on my machine it weird try /testmod instead of /testmode/index and see if that works

just play with the url in the browser try

/testmod # works fine

/testmod/index # throws a 404 you have to add the controller name full path like so /testmod/default/index

/testmod/default/index # works

Weird, indeed. all three versions of the URL return a 404 error on my machine. I tried deploying my project to a different server, but that didn’t seem to produce any change in behavior.

It’s odd that the same code would work on your machine. Arrrggghhhh!

Thanks so much for your help :)

It seems like the request is never getting to the controller file. If I place a print_r(); exit; in the Module.php file, we are getting to the module class and inside the init() method.




namespace cics\modules\testmod;


class Module extends \yii\base\Module

{

	public function init()

	{

		echo '<pre>';

		print_r('testing');

		echo '</pre>';

		exit;


	    parent::init();

	}

}



But if I place a print_r(); exit; in the DefaultController.php file, it seems the 404 error is triggered before the controller file is accessed, because I see the 404 error and not my "testing" printout.




echo '<pre>';

print_r('testing');

echo '</pre>';

exit;


namespace cics\modules\testmod\controllers;


use yii\web\Controller;

use Yii;


class DefaultController extends Controller

{

    /**

     * @return mixed

     */

    public function actionIndex()

    {

        return $this->render('index');

    }

}



I wonder if something is not happening properly in the Module.php init() function? It’s a standard Yii function, but maybe something in my module setup is not letting the module register properly with Yii? Does the “DefaultController.php” file get loaded automatically, just because it’s named Default? Do I have to identify somewhere that I’m using the DefaultController.php file?

Again, any insight or advice anyone can offer is greatly appreciated!

what is the name of the directory that hold the module file. I see you have your testmodule name forum if thats case thats is where your problem is module name in config.php and filename has to be same

I actually copied and pasted the file structure from github, so the “forum” is not the name of my folder. Sorry about that :)

I’m setting this up through packagist.org, so once the module is installed via composer, the file path to the Module.php file is /vendor/cics/yii2-test-mod/Module.php.

I think what I have does have the module name and the file name the same. In my config file, the class reference is (ending in "Module"):




    'modules' => [

        'testmod' => [

                'class' => 'cics\modules\testmod\Module',

        ],

    ],



And then, my filename is Module.php and the class name is also "Module":




namespace cics\modules\testmod;


class Module extends \yii\base\Module

{

	public function init()

	{

	    parent::init();


	}

}



I was thinking that the folder the Module.php file resides in is somewhat irrelevant because I thought it was the module class reference in the /frontend/config/main.php file that tells Yii where the module class is located, and it’s referenced through the namespace not the file path. I’m still learning, so please correct me if I’m misunderstanding things.

If it helps at all to install this module locally to see how I have things set up, you can use the following composer command:




php composer.phar require cics/yii2-test-mod "dev-master"



Thank you SO, SO much for thinking about this with me. I really appreciate it!

I have not tested it I am also shooting in the dark I have a basic yii2 app setup on my machine use it for testing and stuff do you have the package up on packgist paste the link if you do

Here are the links to packagist.org and github.

https://packagist.org/packages/cics/yii2-test-mod

https://github.com/cicsolutions/yii2-test-mod

Thanks again alirz23!