View Path on Gii gives different result

Hi,

I’m having an issue when I set different value on View Path of Gii CRUD generator.

The table name is: mst_employees

First, the View Path is left default (no value) it will create "mst-employees" on my backend views directory.

Then it can be accessed normally at $MYDOMAIN/mst-employees/

But when I set the View Path with “@backend\views\mstemployees” (change mst-employees to mstemployees), it will create mstemployees views directory normally, but I can’t access the path: $MYDOMAIN/mstemployees/

It will create 404 error.

What causing this strange issues?

Thanks,

[bayu]

PS: $MYDOMAIN is the full URL path of my local domain, since this is my first post which not allowed to post URL.

I’m not sure but maybe you must change the default view path in your controller. property $_viewPath. use the method setViewPaht([color="#1C2837"][size=“2”]"@backend\views\mstemployees"[/size][/color][size=“2”]) in you init function.[/size]

It’s because your controller “MstEmployeesController” has the controller ID of ‘mst-employees’.

You have to rename it to “MstemployeesController” if you want its route to be ‘mstemployees’.

http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html#controller-ids

Or, you may customize the mapping between them using Controller Map.

http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html#controller-map

Frankly speaking, since I used Gii, I’m not aware about this “Controller ID”.

I still don’t understand on how we define the controller ID (http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html#controller-ids). Which config file or rules should be defined or declared? or steps on Gii should be filled in.

Hence if you said “MstEmployeesController” has the controller ID of ‘mst-employees’, I still confuses on which part or rules define about it. Or it was an inclusive / default format relates to a table name?

At Gii, I created MstEmployeesController; not Mst-EmployeesController.

Thank you and apologize for late reply.

By default, the controller ID is automatically created by converting the controller’s class name.




class name           | controller ID

---------------------+-----------------

AbcController        | abc

XyzController        | xyz

AbcXyzController     | abc-xyz

AbcXyzLongController | abc-xyz-long



  1. remove "Controller": e.g. AbcXyzController -> AbcXyz

  2. split the word by upper-case letters: e.g. AbcXyz -> Abc, Xyz

  3. convert upper-case letters to lower-case: e.g. Abc, Xyz -> abc, xyz

  4. join words using "-": e.g. abc, xyz -> abc-xyz

Got it? :)

Yes got it :)

Last question, where or when this controller id is declared/defined?

Is it declared hard coded in config file?

Or it was set on executed / running?

Well, the previous description about the rule was not completely correct. It was upside down.

It’s not the controller ID but the controller class name that is automatically generated by convention.




controller ID | class name

--------------+-----------------

abc           | AbcController

xyz           | XyzController

abc-xyz       | AbcXyzController

abc-xyz-long  | AbcXyzLongController



  1. split controller ID by "-" : e.g. abc-xyz -> abc, xyz

  2. capitalize the initial letters : abc, xyz -> Abc, Xyz

  3. join words : e.g. Abc, Xyz -> AbcXyz

  4. append "Controller" : e.g. AbcXyz -> AbcXyzController

The controller ID is given in the request, usually as a part of the url. Then the application will search for the controller that matches the controller ID. Controller Map may be used to override the convention above. When the matching controller has not been found, then 404 exception will be thrown.