The url path using the yii framework is generally like hostname /? R = xxxx/xxxx & sdfs = dsfdsf
We can see that sometimes the controller under the protected directory is used, and sometimes the controller in the module is used. The specific solution is as follows:
The following code is excerpted from yii framework core code % Yiiroot %/framework/web/CWebApplication. php.
Copy codeThe Code is as follows:
========================================================== ========================================================== ========================
// 1. runController is the method for executing a controller, and $ route is $ _ GET ['R']
Public function runController ($ route)
{
// Call createController to create a controller instance first. Therefore, createController is the key to controller selection.
If ($ ca = $ this-> createController ($ route ))! = Null)
{
List ($ controller, $ actionID) = $ ca;
$ OldController = $ this-> _ controller;
$ This-> _ controller = $ controller;
$ Controller-> init ();
$ Controller-> run ($ actionID );
$ This-> _ controller = $ oldController;
}
Else
Throw new CHttpException (404, Yii: t ('yii', 'unable to resolve the request "{route }".',
Array ('{route}' => $ route = ''? $ This-> defaultController: $ route )));
}
========================================================== ========================================================== ============================
// 2. Next we will analyze createController. Assume that the route we access is site/contact.
Public function createController ($ route, $ owner = null)
{
// Enter this function for the first time. The $ owner parameter is blank.
If ($ owner = null)
$ Owner = $ this;
// If the $ route parameter does not contain/, use the default controller
If ($ route = trim ($ route, '/') = '')
$ Route = $ owner-> defaultController;
$ CaseSensitive = $ this-> getUrlManager ()-> caseSensitive;
// Add/
$ Route. = '/';
// Save the location of the slash (/) in $ pos
While ($ pos = strpos ($ route ,'/'))! = False)
{
// $ Id is the first half, that is, site
$ Id = substr ($ route, 0, $ pos );
If (! Preg_match ('/^ \ w + $/', $ id ))
Return null;
If (! $ CaseSensitive)
$ Id = strtolower ($ id );
// $ Route becomes the second half, that is, contact
$ Route = (string) substr ($ route, $ pos + 1 );
// Controller root directory or subdirectory prefix
If (! Isset ($ basePath) // first segment
{
// Enter for the first time. $ owner is empty and does not have this member variable.
// This member variable may not be set for the first entry or $ owner. For more information, see the CWebModule class.
If (isset ($ owner-> controllerMap [$ id])
{
Return array (
Yii: createComponent ($ owner-> controllerMap [$ id], $ id, $ owner ===$ this? Null: $ owner ),
$ This-> parseActionParams ($ route ),
);
}
// If you can obtain an independent module through the getModule method, call createController again. This method is applicable when the site is the module name. For details, see protected/config/main. php configuration file, such as your controller in % webroot %/protected/module/site/controller/ContactController. php
If ($ module = $ owner-> getModule ($ id ))! = Null)
Return $ this-> createController ($ route, $ module );
// Controller directory:
// For CWebApplication, the corresponding config ['basepath'] (see the configuration file)./controller/. For example, if your controller is in % webroot %/protected/controller/SiteController. php
// For the CModule subclass, change the folder where the subclass is located./contoller/. For example, if your controller is in % webroot %/protected/module/site/controller/ContactController. php
$ BasePath = $ owner-> getControllerPath ();
$ ControllerID = '';
}
Else
$ ControllerID. = '/';
$ ClassName = ucfirst ($ id). 'controller ';
$ ClassFile = $ basePath. DIRECTORY_SEPARATOR. $ className. '. php ';
// If $ classFile exists, create a class instance based on the path of the controller class file obtained above
// If it does not exist, it is the controller in the subdirectory. continue to search for the final controller. For example, if your controller is in % webroot %/protected/controller/somedir/SiteController
If (is_file ($ classFile ))
{
If (! Class_exists ($ className, false ))
Require ($ classFile );
If (class_exists ($ className, false) & is_subclass_of ($ className, 'ccontroller '))
{
$ Id [0] = strtolower ($ id [0]);
Return array (
New $ className ($ controllerID. $ id, $ owner ===$ this? Null: $ owner ),
$ This-> parseActionParams ($ route ),
);
}
Return null;
}
$ ControllerID. = $ id;
$ BasePath. = DIRECTORY_SEPARATOR. $ id;
}
}