yii framework source code analysis for creating controller code
- 2020-05-09 18:14:47
- OfStack
The url path 1 using the yii framework looks like hostname/? r = xxxx xxxx/xxxx & sdfs = dsfdsf
We can see that controller in protected directory is sometimes used, and controller in module is sometimes used. For details, please refer to the following analysis:
The following code excerpt from yii framework Yiiroot core code % % / framework web/CWebApplication php
We can see that controller in protected directory is sometimes used, and controller in module is sometimes used. For details, please refer to the following analysis:
The following code excerpt from yii framework Yiiroot core code % % / framework web/CWebApplication php
=================================================================================================
//1.runController Is to perform 1 a controller The method of ,$route is $_GET['r']
public function runController($route)
{
// I'm going to call createController Go to create 1 a controller Example, so to speak createController Is to choose controller The key to
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. So let's analyze createController, So let's say we're visiting route is site/contact
public function createController($route,$owner=null)
{
// The first time I enter this function, $owner Parameter is null
if($owner===null)
$owner=$this;
// if $route Parameter is not included / , then use the default controller
if(($route=trim($route,'/'))==='')
$route=$owner->defaultController;
$caseSensitive=$this->getUrlManager()->caseSensitive;
// To be able to fully run the following loop, give $route After it 1 a /
$route.='/';
// will / The location is saved at $pos In the
while(($pos=strpos($route,'/'))!==false)
{
//$id It's the first half, which is site
$id=substr($route,0,$pos);
if(!preg_match('/^\w+$/',$id))
return null;
if(!$caseSensitive)
$id=strtolower($id);
//$route Into the second half, which is contact
$route=(string)substr($route,$pos+1);
//controller Root or subdirectory prefix
if(!isset($basePath)) // first segment
{
// First entry, $owner Is null without this member variable
// Not first entry or $owner There are values, and it's possible that this member variable is set. See also 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 get it through getModule Method to get 1 Two independent modules, then call again createController , and is suitable for site is module Name of the case, reference protected/config/main.php Configuration files, such as yours controller in %webroot%/protected/module/site/controller/ContactController.php
if(($module=$owner->getModule($id))!==null)
return $this->createController($route,$module);
//controller The directory:
// for CWebApplication , corresponding to config['basePath']( See configuration files )./controller/ , such as yours controller in %webroot%/protected/controller/SiteController.php
// for CModule Subclass, corresponding to the subclass folder ./contoller/ , such as yours 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 There is, according to what we have above controller Class file path, create class instance
// If it doesn't exist, it's in a subdirectory controller , continue the loop to find 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;
}
}