Changing Controllers Name

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

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.

Default situation

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}/{controller_id}/default/index.html
Changing Controller Name

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:

  1. Change controller's file name, for example from SiteController.php to _MainController.php".
  2. Edit this controller file and change class name, in the first code line, for example from SiteController to _MainController" (see end of this chapter).
  3. Change views folder name, for example from protected/views/site to protected/views/main.
  4. If this is main (default, major) controller, edit application configuration file and inform Yii about change in application or module default controller name.

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
Final words

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!

4 0
2 followers
Viewed: 18 184 times
Version: Unknown (update)
Category: How-tos
Written by: Trejder
Last updated by: Trejder
Created on: Dec 9, 2010
Last updated: 5 years ago
Update Article

Revisions

View all history

Related Articles