YII Source Code Analysis (III), yii Source Code Analysis _ PHP Tutorial

Source: Internet
Author: User
YII Source Code Analysis (3): yii source code analysis. YII Source Code Analysis (3). yii source code analysis has already completed the process to start a yii program and how to render a page. What we want to analyze today is yii, for example, YII Source Code Analysis (3). yii source code analysis

We have already finished the process for starting a yii program and how to render a page. What we need to analyze today is how yii processes user requests. That is, control and action.

Take helloworld as an example to demonstrate this process. In the address bar, enter http: // localhost/study/yii/demos/helloworld/index. php. the page displays hello world.

The preceding analysis uses the default values. However, if a url has a parameter, how does yii handle it? Let's take a look at this question.

There is a line of code in CWebApplication:

$route=$this->getUrlManager()->parseUrl($this->getRequest());

This is the legendary route. Is it a little frozen? Let's take a look at getUrlManager.

    public function getUrlManager()    {        return $this->getComponent('urlManager');    }

You need to find the link again.

    public function getComponent($id,$createIfNull=true)    {        if(isset($this->_components[$id]))            return $this->_components[$id];        elseif(isset($this->_componentConfig[$id]) && $createIfNull)        {            $config=$this->_componentConfig[$id];            if(!isset($config['enabled']) || $config['enabled'])            {                Yii::trace("Loading \"$id\" application component",'system.CModule');                unset($config['enabled']);                $component=Yii::createComponent($config);                $component->init();                return $this->_components[$id]=$component;            }        }    }

After the return $ this-> _ components [$ id] is executed, the id is the uploaded urlManager. In fact, there is nothing to see here. you can directly find the urlManager class and check the parseUrl:

    public function parseUrl($request)    {        if($this->getUrlFormat()===self::PATH_FORMAT)        {            $rawPathInfo=$request->getPathInfo();            $pathInfo=$this->removeUrlSuffix($rawPathInfo,$this->urlSuffix);            foreach($this->_rules as $i=>$rule)            {                if(is_array($rule))                    $this->_rules[$i]=$rule=Yii::createComponent($rule);                if(($r=$rule->parseUrl($this,$request,$pathInfo,$rawPathInfo))!==false)                    return isset($_GET[$this->routeVar]) ? $_GET[$this->routeVar] : $r;            }            if($this->useStrictParsing)                throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',                    array('{route}'=>$pathInfo)));            else                return $pathInfo;        }        elseif(isset($_GET[$this->routeVar]))            return $_GET[$this->routeVar];        elseif(isset($_POST[$this->routeVar]))            return $_POST[$this->routeVar];        else            return '';    }

From the code above, if we do not upload something in the url, return ''directly. So the question is, how can we transmit parameters?

isset($_GET[$this->routeVar]) 

public $routeVar='r';

So there is a solution. let's make it worse together. Add such a parameter helloworld/index. php? R = abc

An error is reported. It indicates that the abc controller does not exist. In fact, it also does not exist. it makes a little bad. it is called that a man is not bad and a woman does not love it.

Changed to helloworld/index. php? R = site can display hello world. what is the concept of this ghost? The reason is simple, because the site controller is defined.

class SiteController extends CController{    /**     * Index action is the default action in a controller.     */    public function actionIndex()    {        echo 'Hello World';    }}

Okay, I have no opinion on this, but is actionIndex a ghost? In yii, this is called an action. It captures the parameters following the controller? R = site/index is the index, and actions are separated by "/". to verify that what I am talking about is not a lie to girls, let me add an action to the site controller to show you:

class SiteController extends CController{    /**     * Index action is the default action in a controller.     */    public function actionIndex()    {        echo 'Hello World';    }    public function actionView()    {        echo 'Hello View';    }}

Access? When r = site/view, does the output 'Hello view' appear? Yes. although I read a few books, you cannot lie to me. there is a picture of the truth:

I don't like the site name at all. test is my favorite, so I created another test controller to try it.

What is the definition of actions? I just tried it to know that it is actually another representation.

I remember it was used in the blog example to display the verification code:

    /**     * Declares class-based actions.     */    public function actions()    {        return array(            // captcha action renders the CAPTCHA image displayed on the contact page            'captcha'=>array(                'class'=>'CCaptchaAction',                'backColor'=>0xFFFFFF,            ),            // page action renders "static" pages stored under 'protected/views/site/pages'            // They can be accessed via: index.php?r=site/page&view=FileName            'page'=>array(                'class'=>'CViewAction',            ),        );    }

I understand it as a set of actions that collectively declare third-party businesses. because of the actions in this controller, I think the action + ID method is direct.

What? What do you mean I use index. php/site/captcha instead of index. php? R = site/captcha. This should start with the configuration file.

        'urlManager'=>array(            'urlFormat'=>'path',            'rules'=>array(                'post/
 
  /
  
   '=>'post/view',                'posts/
   
    '=>'post/index',                '
    
     /'=>'
     
      /',            ),        ),
     
    
   
  
 

UrlFormat has two types: path and get. if it is not specified in main. php, it is the get method, that is, index. php? R = site/captcha. If this parameter is specified, that is, index. php/site/captcha

Literally, path is like the path format, and get is? This form.

There are still many content about routing and controller, but this section is here.

Http://www.bkjia.com/PHPjc/930706.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/930706.htmlTechArticleYII Source Code Analysis (III), yii source code analysis has been read before starting a yii program to go through the process, and rendering a page is how to complete. What we want to analyze today is that yii is like...

Related Article

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.