URL paths that use the YII framework are generally shaped like HOSTNAME/?R=XXXX/XXXX/XXXX&SDFS=DSFDSF
We can see that sometimes we use the controller in the protected directory, and sometimes we use the controller in module, how do we deal with it, see the following analysis:
The following code is excerpted from the YII Framework core code%yiiroot%/framework/web/cwebapplication.php
Copy Code code as follows:
=================================================================================================
1.runController is the method of executing a controller, $route is $_get[' R ']
Public Function Runcontroller ($route)
{
Call Createcontroller here first to create a controller instance, this shows that Createcontroller is the key to choosing controller
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 ' request ' {route} '.
Array (' {route} ' => $route = = '? $this->defaultcontroller: $route)));
}
==================================================================================================
2. Next we analyze createcontroller, assuming that the route we visit is Site/contact
Public Function Createcontroller ($route, $owner =null)
{
Enter this function for the first time, $owner parameter is empty
if ($owner ===null)
$owner = $this;
If the $route parameter does not contain/, then use the default controller
if ($route =trim ($route, '/') = = = ")
$route = $owner->defaultcontroller;
$caseSensitive = $this->geturlmanager ()->casesensitive;
In order to be able to run the following loop completely, add a/$route to the back of the
$route. = '/';
Save/position in $pos
while (($pos =strpos ($route, '/'))!==false)
{
$id is the first half, site
$id =substr ($route, 0, $pos);
if (!preg_match ('/^\w+$/', $id))
return null;
if (! $caseSensitive)
$id =strtolower ($id);
$route into the second half, that is contact
$route = (string) substr ($route, $pos + 1);
Controller root directory or subdirectory prefix
if (!isset ($basePath))//Segment
{
First entry, $owner NULL, no this member variable
It is possible to set this member variable if it is not first entered or $owner, see Cwebmodule class
if (Isset ($owner->controllermap[$id]))
{
Return Array (
Yii::createcomponent ($owner->controllermap[$id], $id, $owner = = = $this? Null: $owner),
$this->parseactionparams ($route),
);
}
If a standalone module can be obtained through the GetModule method, then the Createcontroller is called again, which is applicable to the case where site is the module name, reference protected/config/ main.php configuration files, such as your controller in%webroot%/protected/module/site/controller/contactcontroller.php
if (($module = $owner->getmodule ($id))!==null)
return $this->createcontroller ($route, $module);
Controller's Directory:
For cwebapplication, correspond to config[' BasePath ' (see Configuration file)./controller/, for example your controller in%webroot%/protected/controller/ sitecontroller.php
For Cmodule subclasses, the folder where the subclass is located./contoller/, for example your controller 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 Controller class file path above
If it does not exist, it is the controller under the subdirectory, and continue to loop for the final controller, such as your controller 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;
}
}