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!