Yii Framework source code analysis: creating controller code

Source: Internet
Author: User
We can see that sometimes the controller under the protected directory is used, and sometimes the controller in the module is used. how can this problem be solved, see the following analysis. 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.

The 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;
}
}

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.