yii2: problem with modules
hi, i have a problem in this case i have this structure of directory
-module –user —controller ----default ----profile ----country —view …
when i try use this url (example) http://localhost/user/profile i get 404 but when i use http://localhost/user/profile/index this work…
this is my urlmanager
'urlManager'=>[
//'urlFormat'=>'path',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules'=>[
'<module:\w+>/<id:\d+>' => '<module>/default/view',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
],
],
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 30 (13 by maintainers)
This is too much magic - this should not be default behavior. But I think that it’s worth to add switch like
hideDefultControllerRoute
to module which enables such behavior only for this module.And then if I remove
index
action fromProfileController
URL forprofile
action inDefaultController
will magically change and URL/user/profile
will show different content?If it exists, yeah.
Look,
/user/index
works automatically with new Gii build. If you add another action to DefaultController, saytest
,/user/test
does not work, and it SHOULD! Because it has been namedDefaultController
. So if it isn’t going to act like a default controller, then it should be renamed to something not as misleading, likeMainController
.It wouldn’t be hard for it to check if the controller exists, then fall back to
DefaultController
if it doesn’t, and a 404 if it still fails.This feature should be easy to turn off, and the “default” controller should be able to be changed. Either via in the main config, or overridden like we can with
layout
.Sorry to bump this, but I have a suggestion. I am not sure if it does exist (now) since this is an older post.
When you create a “Module” with Gii, it creates DefaultController with the action of “index” and the view “index”.
Logically thinking, we access the site via: /controller/action In the case of modules, the name of the module is in place of the controller: /module/***
Out of the box, if you just go to
/module
-> it automatically loads the “index” action and view. So it KNOWS how to find the default controller and index action, even though we didn’t specify. So behind the scenes, it’s passing the request to the DefaultController. Why does it not pass any other action to the DefaultController too?In DefaultController I added another action,
actionBrowse()
. When I go to: /module/browse, one would expect it would run the browse action inside DefaultController. However, it throws a 404. While, /module/default/browse works fine, as expected.I do not think we should have to set up a rules in the url manager. I prefer to NOT mess with the rules unless I absolutely have to.