This very short how-to presents steps required to change controller name. It is oriented for less experienced users, which are starting their journey with Yii, to avoid common newbie pitfalls.
If you start with scratch application, generated by using yiic script (in framework base directory) along with webapp parameter and path, where new application should be created, you will get an application with one controller (class SiteController defined in SiteController.php and placed in protected/controllers folder). If you use Gii tool for creating module inside existing application, it will be created also with one controller and this time it will be DefaultController class, defined in DefaultController.php and placed in protected/modules/{moduleId}/controllers folder.
For above mentioned controllers, you will also get a set of pre-generated view files. Those will be files placed in protected/views/site folder for main application and protected/modules/{moduleId}/views/default for module (if you have any).
If you are using standard (autogenerated) configuration file, there are some default URL-rewriting rules and application and/or module can be accessed via URLs like these:
http://{server_address}/{application_path}/site/index.html http://{server_address}/{application_path}/{moduleId}/default/index.html
If you wish to change not only URLs (which can be done with URL Management --> see here), but also controllers names (for example for some files and folders structure cleanup or for satisfying defined name nomenclature), you will have to undertakes theses four steps:
For application:
return array ( //Base settings 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'defaultController'=>'main', ... }
For module:
return array ( ... //Aplication modules 'modules'=>array ( 'admin'=>array ( 'defaultController'=>'go', ... ), ... ), ... }
P.S.: Step 2, you're changing controllers calls name in this line:
class DefaultController extends Controller
Some Yii forum evidences shows that most newbie users forgot about second step (changing class name in class definition of controller file), which - when omitted - will result in "Unable to resolve the request moduleId." error.
Please, feel free to update this article, if you find some mistakes in it or if you find, that something is missing.
P.S.: Dots in above examples are placeholders for other parts of code in configuration file, do not actually include them in your config file!
Be the first person to leave a comment
Please login to leave your comment.