The source code Analysis of YII framework Create a Controller _php tutorial

Source: Internet
Author: User
Tags yii
URL paths using the YII framework are generally shaped like HOSTNAME/?R=XXXX/XXXX/XXXX&SDFS=DSFDSF

We can see that sometimes using the controller in the protected directory, sometimes using the controller in the module, specifically how to handle it, see the following analysis:

The following code is excerpted from the YII Framework core code%yiiroot%/framework/web/cwebapplication.php
Copy CodeThe code is 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 choose 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 the request ' {route} ',
Array (' {route} ' = = $route = = = '? $this->defaultcontroller: $route)));
}
==================================================================================================
2. Next we analyze createcontroller, assuming that the route we are visiting 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 the default controller is used
if ($route =trim ($route, '/')) = = = = ")
$route = $owner->defaultcontroller;
$caseSensitive = $this->geturlmanager ()->casesensitive;
In order to be able to fully run the following loop, add a/$route behind
$route. = '/';
Save/position in $pos
while (($pos =strpos ($route, '/'))!==false)
{
$id is the first half, that is, the site
$id =substr ($route, 0, $pos);
if (!preg_match ('/^\w+$/', $id))
return null;
if (! $caseSensitive)
$id =strtolower ($id);
The $route becomes the second part
$route = (string) substr ($route, $pos + 1);
Controller root directory or sub directory prefix
if (!isset ($basePath))//First segment
{
First entry, $owner empty, without this member variable
Non-first entry or $owner has a value, it is possible to set this member variable, see 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 a standalone module through the GetModule method, call Createcontroller again, for the case where site is a module name, refer to 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 is in%webroot%/protected/controller/ sitecontroller.php
For Cmodule subclasses, correspond to the folder where the subclass is located./contoller/, for example, 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, the class instance is created based on the Controller class file path obtained above
If it does not exist, it is the controller in the subdirectory and continues 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;
}
}

http://www.bkjia.com/PHPjc/323762.html www.bkjia.com true http://www.bkjia.com/PHPjc/323762.html techarticle URL paths using the YII framework are generally shaped like Hostname/?r=xxxx/xxxx/xxxxgt;createcontroller ($route))!==null) {list ($controller, $actionID ) = $ca; $oldController = $this-_controller; $this-_con ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.