Index view urlCreator

It seems that I have to change the view files and I don’t know why? I used Gii to generate the models and CRUD…

My view file “index.php” for Users has:
‘urlCreator’ => function ($action, Users $model, $key …
And I have to change it to
‘urlCreator’ => function ($action, \app\models\Users $model, $key

How to I prevent having to change every view file? so I can use “Users $model” instead of “\app\models\Users $model” ?

J

I don’t understand your explanation and the question.
May be if you take time to write legible question you will get decent answers!

also you can import the Users class with a use expression, normally at the top of the file.

It seems that the Gii template you’re using is not using the fully name of the class when generating the code. So, you can modify the template OR do the change manually every time.

1 Like

Maybe you shouldn’t bother posting useless crap.
My question was clear if you read the subject line.

How do I change the behavior of the template?

https://www.yiiframework.com/extension/yiisoft/yii2-gii/doc/guide/2.2/en/topics-creating-your-own-templates

2 Likes

I guess I am confused as to why something needs to be changed. When I used Gii to create CRUD, why do I need to edit any of the files for them to work?

At the bottom of the gridview section in the index.php view in the urlCreator I see…
‘urlCreator’ => function ($action, Schools $model, $key, $index, $column) {
return Url::toRoute([$action, ‘school_id’ => $model->school_id]);

It won’t work if I navigate to the Schools index view unless I change it to …
‘urlCreator’ => function ($action, app\models\Schools $model, $key, $index, $column) {
return Url::toRoute([$action, ‘school_id’ => $model->school_id]);

My yii2 was just installed using composer so it should be up to date. Why is this happening and why do I need to alter the template for this to work?

So doing more digging I found a post that suggests this is a bug in yii2?

If this is indeed true, then I went to the page about Gii extensions

and found an error in the docs…

If you open the folder @app\vendor\yiisoft\yii2-gii\generators , you’ll see six folders of generators.

This path does not exist, but @app\vendor/yiisoft/yii2-gii/src/generators does.

Should I add a use statement near the current use statements?

use app\models$modelClass

Or add app\models\ before the $modelClass in the urlCreator?

Or did I miss something in the earlier portion of the docs in my configuration that is causing this?

Just a bump to get a solution? Just tried another fresh install and same thing happens. I’m using Ubuntu 20.04…

Never created a custom template but I would try:

Adding use <?= $generator->modelClass ?>; near line 14-17 of the template

or just try

‘urlCreator’ => function ($action, <?= $generator->modelClass ?> $model, $key, $index, $column) {
return Url::toRoute([$action, <?= $generator->generateUrlParams() ?>]);

(near line 69-70)

source reference https://github.com/yiisoft/yii2-gii/blob/master/src/generators/crud/default/views/index.php

So before you replied I added use app\models<?= $modelClass ?>; Is this any different? It did seem to fix it.