Home  >  Article  >  Backend Development  >  Yii controller action parameter binding processing_PHP tutorial

Yii controller action parameter binding processing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:40877browse

Yii controller action parameter binding processing

Starting from version 1.1.4, Yii provides support for automatic action parameter binding. That is, controller actions can define named parameters whose values ​​will be automatically populated by Yii from $_GET.

To illustrate this functionality in detail, assume we need to write a create action for PostController. This action requires passing two parameters via $_GET:

category: an integer representing the ID of the category in which the post is to be published.

language: A string representing the language code used by the post.

When extracting parameters from $_GET, we can no longer write the relevant verification code like the following:

class PostController extends CController{
	public function actionCreate(){
		if(isset($_GET['category']))
			$category=(int)$_GET['category'];
		else
			throw new CHttpException(404,'invalid request');
		if(isset($_GET['language']))
			$language=$_GET['language'];
		else
			$language='en';
		// ......
	}
}

Now using the action parameter function, we can more easily complete tasks related to the above code:

class PostController extends CController{
	public function actionCreate($category, $language='en'){
		$category = (int)$category;
		echo 'Category:'.$category.'/Language:'.$language;
		// ......
	}
}

Note that we added two parameters to the action method actionCreate. The names of these parameters must match the names we want to extract from $_GET. When the user does not specify the $language parameter in the request, this parameter will use the default value en . Since $category has no default value, if the user does not provide the category parameter in $_GET, a CHttpException (error code 400) will be automatically thrown.

Starting from version 1.1.5, Yii already supports array action parameters. How to use:

class PostController extends CController{
	public function actionCreate(array $categories){
		// Yii will make sure $categories be an array
	}
}

Articles you may be interested in

  • Solution to the "Invalid parameter encountered" error pops up when CuteFTP connects to the ftp server
  • linux chmod (file or file Detailed explanation of command parameters and usage
  • Summary of system constants in thinkphp’s Action controller
  • JavaScript browser address bar special effects: Invincible Hot Wheels special effects
  • Left and right scrolling based on jQuery control, automatic scrolling effect
  • Windows cannot start this hardware device because its configuration information (in the registry) is incomplete or damaged. (Code 19) Solution
  • Javascript method to obtain the current complete url address and parameters
  • PHP function to check browser parameters to prevent SQL injection

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/980453.htmlTechArticleYii controller action parameter binding processing Starting from version 1.1.4, Yii provides automatic action parameter binding support. That is, controller actions can define named parameters, the values ​​of which...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn