Gii generates code that Yii2 doesn't like

I’ve generated 8 models with Gii. The first 6 are great, and work as expected, but the last two give me a 404 error when I try to go to their index page.

The only thing different from the 6 that work from the two that don’t is that the working 6 have one-word table names and titles (like Teacher, Inventory, etc.). The “broken” ones have two-word titles like training_description and food_preference.

Now, the short answer is of course to re-generate the tables and code with single-word names, but that isn’t really answering my question. In order to move ahead, that’s what I’m going to do, but I’d still like to know what I’ve done wrong so I can use descriptive names in the future.
Thanks for any help.

Check the url manager’s rules. Probably their patterns are not adjusted to match ‘two-word’ controller IDs like ‘training-description’ and ‘food-preference’.

2 Likes

Thanks, softark. I checked my url rules and they don’t accommodate hyphens. I tried a few configurations but nothing worked… what would be a valid url rule for a hyphenated controller name?
Here’s my current rule set:

‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’=>false,
‘rules’ => [
‘<controller:\w+>/<id:\d+>’=>’<controller>/view’,
‘<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’,
‘<controller:\w+>/<action:\w+>’=>’<controller>/<action>’,
],
],

I’m using Yii2.0.6, PHP 5.6.37

You can try this:

‘rules’ => [
    ‘<controller:[\w-]+>/<id:\d+>’=>’<controller>/view’,
    ‘<controller:[\w-]+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’,
    ‘<controller:[\w-]+>/<action:\w+>’=>’<controller>/<action>’,
],

When you have hyphenated action IDs, change also the patterns for action.

I have figured out what I did wrong and how to fix it, but I don’t understand precisely why it happens.
When I used Gii to generate my models it worked fine. Then I generated my Ajax CRUD, but here I mis-named various parts of the puzzle.
According to this link (thanks, akhil!), I should have named my tables, models, controllers, and views differently.

For brevity, I’m only going to discuss the solution for training-description. The solution for food-preference is the same.

I deleted my database tables and my code models, controllers, and views and started over from scratch. I changed my migrations to use table names with underscores (for some reason Gii doesn’t like hyphens in table names):
training_description

then I used Gii’s model generator to create the models as:
frontend\models\TrainingDescription

Then I used Gii’s Ajax CRUD generator to create the controllers and views with these settings:
Model Class: frontend\models\TrainingDescription
Search Model Class: frontend\models\TrainingDescriptionSearch
Controller Class: frontend\controllers\TrainingDescriptionController
View Path: @frontend/views/training-description

Now, when I make a layout menu item pointing to the index page for training-description, it works!

Blockquote
[
‘label’ => ‘Training’,
‘items’=> [
[‘label’=>‘Training Types’, ‘url’ => [’/training-description’]],
[‘label’=>‘Food Preferences’,‘url’ => [’/food-preference’]],
],
‘visible’ => !Yii::$app->user->isGuest,
],

Thanks for your help, softark! Your advice led me to read the Routing and URL Creation Guide, which led to the solution.