Home » Php » php – Yii 2 gii is generating view files on different directory

php – Yii 2 gii is generating view files on different directory

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am new to yii framework. On yii version:2.0.1 I have created a module in which I tried to generate a CRUD model using the gii functionality.
After putting the model class, controller class and view path when I clicked on generate gii showed all the files has been created successfully.
But when I tried to view, below message has been shown to me,

The view file does not exist :

C:\xampp\htdocs\advanced\backend\modules\settings\views\companies\index.php

I found the view files in web directory not in the path I entered. Hence the error.

Here is my inputs to gii,

Model Class:

backend\modules\settings\models\Companies

Search Model Class:

backend\modules\settings\models\CompaniesSearch

Controller Class:

backend\modules\settings\controllers\CompaniesController

View Path:

backend\modules\settings\views\companies

When I click generate I have given below message :

Generating code using template "C:\xampp\htdocs\advanced\vendor\yiisoft\yii2-gii\generators\crud/default"…

generated modules\settings\controllers\CompaniesController.php

generated modules\settings\models\CompaniesSearch.php

generated backend\modules\settings\views\companies\_form.php

generated backend\modules\settings\views\companies\_search.php

generated backend\modules\settings\views\companies\create.php

generated backend\modules\settings\views\companies\index.php

generated backend\modules\settings\views\companies\update.php

generated backend\modules\settings\views\companies\view.php

done!

Does anybody have any idea why is it happening.

Thanks in advance.

How to&Answers:

I solved similar problem by changing

backend\modules\settings\views\companies

into

@backend/modules/settings/views/companies

Hopefully it helps someone in future

Answer:

You missed one final folder and alias in View Path. It should be @backend\modules\settings\views\companies.

As you can see in creation log, the generated files are in wrong place (root views folder), that’s why the error is thrown.

Answer:

Have the same problem. Solved entering @backend/modules/settings/views/companies instead backend\modules\settings\views\companies.
See on slashes.

Answer:

For view path in CRUD generator, enter the absolute or full path. For example

/home/developer/workspace/advanced/backend/views/<your view folder> 

Answer:

put this in the VIEW PATH

@backend/modules/settings/views/companies

and it’s done!

Answer:

I have tried with absolute path i,e

C:/xampp/htdocs/advanced/backend/modules/settings/views/companies

It worked for the absolute path.

Answer:

I tried this is working by entering:
/Applications/XAMPP/htdocs/advanced/backend/modules/settings/views/companies

I hope you can do it well.

Answer:

I’m using the advanced theme

Here is my Gii setup in case it is helpful for anyone

module generator
----------------
module class:       backend\modules\posts\Module
module id:          posts

model generator
---------------
table name:         posts
model class:        Posts
namespace:          backend\modules\posts\models
Enable I18n:        checked. category: app

CRUD generator
--------------
Model class:        backend\modules\posts\models\Posts
Search model class: backend\modules\posts\models\PostsSearch
Controller class:   backend\modules\posts\controllers\PostsController
View path:          @backend/modules/posts/views/posts
Enable I18n:        checked. category: app
Enable pjax:        checked

If you are using some non-default user management (like amnah module), 
you need to change Users::className() in the models\Posts.php  to
\amnah\yii2\user\models\User::className()

Answer:

Previously I put a model into the incorrect directory when wanting to achieve an absolute path (address it absolutely as it seems to be more often functional) –

app/backend/modules/settings .

Notice the ‘app’ in the beginning (the /app is the main directory in Yii starter kit) which I thought would avoid the relative path but must NOT be there nor @mail sign, but on the contrary, it did the opposite – it appended it relatively with its absolute length to the /document root directory, basically duplicated it,

"app/myproject.com/app/backend/modules/settings"

So it unwinds from the model location, the causes of Gii complaints about incorrect paths or put also controllers the incorrect way.

Also strangely enough for views in contrast to models, it had to be put the upper mentioned different way with the @ relative reference sign annotation (which was not allowed to be used for controllers or models at the time of writing)

@backend/modules/settings/views/companies, otherwise it appended the directory tree into the document root, again concatenated – the web directory of backend backend/web (backend/modules/settings/views/companies)