What is $_GET['tag'] in Blog Demo

This is the code of the index action in the Blog demo:


public function actionIndex()

{

    $criteria=new CDbCriteria(array(

        'condition'=>'status='.Post::STATUS_PUBLISHED,

        'order'=>'update_time DESC',

        'with'=>'commentCount',

    ));

    if(isset($_GET['tag']))

        $criteria->addSearchCondition('tags',$_GET['tag']);

 

    $dataProvider=new CActiveDataProvider('Post', array(

        'pagination'=>array(

            'pageSize'=>5,

        ),

        'criteria'=>$criteria,

    ));

 

    $this->render('index',array(

        'dataProvider'=>$dataProvider,

    ));

}

One thing troubles a lot is the


$_GET['tag']

. It’s not how it works in the PHP manual.

According to PHP manual, tag should be be in the


<input name = "tag"...> </input>,

but I couldn’t find any ‘tag’ in the view file of index action of the PostController.

Can you tell me what’s wrong?

Thank you!

I don’t think so…

$_GET[‘tag’] is not supposed inside the <input name=“tag” />, because if so, it should be $_POST[‘tag’].

This $_GET[‘tag’] is supposed you are clicking one of the tag, and it will list down all the blog post with corresponding tag.

For example you are clicking "sport" tag…

then the URL will look like this

www.yourweb.com/index.php?r=post/index&tag=sport

In the controller to retrieve the tag value (which is “sport”), we need to write $_GET[‘tag’]. Use that value and do query then display the result.

NB: If you try blog demo on your localhost, you might want to disable the URL Manager first, to see the url that I just wrote. Because by default the example enables the url manager. Go to /protected/config/main.php

and comment this line




        'urlManager'=>array(

        	'urlFormat'=>'path',

        	'rules'=>array(

        		'post/<id:\d+>/<title:.*?>'=>'post/view',

        		'posts/<tag:.*?>'=>'post/index',

        		'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

        		'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',

        	),

        ),



Isn’t the following URL is the result of form with action method = GET?


www.yourweb.com/index.php?r=post/index&tag=sport

My confusion is that I couldn’t find any form in the final HTML code.

This is the actual HTML code about tag:


<div class="portlet-content">

<span class="tag" style="font-size:9pt"><a href="/yii/demos/blog/index.php/posts/blog">blog</a></span>

<span class="tag" style="font-size:9pt"><a href="/yii/demos/blog/index.php/posts/test">test</a></span>

<span class="tag" style="font-size:9pt"><a href="/yii/demos/blog/index.php/posts/yii">yii</a></span>

</div>

It’s just a link, no form. How could this <a> tag generate a $_GET array? Only <form> with method = GET can generate a $_GET array. Is it right?

Please let me know your thinking. Thank you!

If you take a look at the code of CUrlRule you can find this:





public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)

	{

		//...


		if(preg_match($this->pattern.$case,$pathInfo,$matches))

		{

			foreach($this->defaultParams as $name=>$value)

			{

				if(!isset($_GET[$name]))

					$_REQUEST[$name]=$_GET[$name]=$value;

			}

			$tr=array();

			foreach($matches as $key=>$value)

			{

				if(isset($this->references[$key]))

					$tr[$this->references[$key]]=$value;

				else if(isset($this->params[$key]))

					$_REQUEST[$key]=$_GET[$key]=$value;

			}

			if($pathInfo!==$matches[0]) // there're additional GET params

				$manager->parsePathInfo(ltrim(substr($pathInfo,strlen($matches[0])),'/'));

			if($this->routePattern!==null)

				return strtr($this->route,$tr);

			else

				return $this->route;

		}


		//...

	}



There’s no difference between a form with method=“get” and a link with a query string. Both can generate the same $_GET data.

Thanks a lot! This is new to me. Is this knowledge well known to other people?