YII Source Analysis (3)

Source: Internet
Author: User
Analysis of YII source code (III.)

We have seen the process of starting a Yii program and how to render a page. Today's analysis is about how Yii handles user requests. Which is the control and Action section.

Or take HelloWorld as an example to demonstrate this process. We enter http://localhost/study/yii/demos/helloworld/index.php in the Address bar and the page shows Hello world.

The previous analysis is the default value, but if the URL has parameters, how does yii deal with it? With this problem, we will analyze it concretely.

There is such a line of code in Cwebapplication:

$route=$this->geturlmanager ()->parseurl ($this->getrequest ());

This is the legendary route, is not a little chicken frozen it? First look at Geturlmanager is a god horse.

     Public function Geturlmanager ()    {        return$this->getcomponent (' Urlmanager ');    }

This is going to go through the relationship again.

     Public functionGetcomponent ($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; }        }    }

Executed the return $this->_components[$id]; ID is passed in the Urlmanager, in fact, from here also can not see what, directly find Urlmanager this class, see parseURL:

     Public functionparseURL ($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 NewChttpexception (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''; }

Judging from the code above, if we don't upload something in the URL, we'll just return it. So the question came, how to pass the parameters?

Isset ($_get[$this

 Public $routeVar= ' R ';

So there is a way, let us make a bad point. Add one of these parameters helloworld/index.php?r=abc

Found an error. ABC This controller does not exist, in fact, it does not exist, so little bad bad, is so-called men are not bad, women do not love.

Change to Helloworld/index.php?r=site can show Hello World, what is this principle? The reason is simple because the site controller is defined.

class extends ccontroller{    * * *Index action is the     default action in a controller.      */     Public function Actionindex ()    {        echo ' Hello world ';    } }

Well, I don't have a problem with that, but is Actionindex a ghost? In Yii, this is called an action. It captures the parameters behind the controller, and if we lose? R=site/index is the index, the action is separated by "/", in order to check what I said is not cheat girl's story, I am in the site Controller add an action to you to see:

class extends ccontroller{    * * *Index action is the     default action in a controller.      */     Public function Actionindex ()    {        echo ' Hello world ';    }      Public function Actionview ()    {        echo ' Hello View ';    }}

When visiting the R=site/view, did you see the output ' Hello View '? Sure yes, although I read less books, but you can not deceive me, there is a picture of the truth:

I do not like to use the name of the site, test is my favorite, so I built a test controller to try.

Sharp-eyed must see how to write an actions, what is this ghost? I also just tried to know, it is actually another way of expression.

I remember in the blog that example has been useful to display the verification code:

    /** * Declares class-based actions. */     Public functionactions () {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 centralized statement of the action set of a third-party business, because the actions within this controller, I think, are action+id in a direct way.

What the heck? You said I was using Index.php/site/captcha instead of Index.php?r=site/captcha. And that's going to start with the config file.

        ' Urlmanager ' = =array            (' urlformat ' = ' path ',            ' rules ' = =array(                ' post/
 
  
   
  /
  
   
    
   ' = ' post/view ',                ' posts/' and '
   
    
     
    post/index ' ,                '
    
     
      
     /' = '
     
      
      
        /
     ', '
        )
      
    
      ,
   
    
  
   
 
  

Urlformat has path and get two kinds, if not specified in the main.php, then is the Get method, that is Index.php?r=site/captcha this. If specified, this is Index.php/site/captcha

It is also literally well understood that path is a path-like format, and get is this form.

There are a lot of things about the Routing and Controller section, but this is the section.

  • 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.