Yii controller action parameter binding processing _ PHP Tutorial

Source: Internet
Author: User
Yii controller action parameter binding process. Yii controller action parameter binding starts from version 1.1.4. Yii provides support for automatic action parameter binding. That is to say, the controller action can define the named parameters. The value of the parameter is the Yii controller action parameter binding process.

Starting from version 1.1.4, Yii provides support for binding automatic action parameters. That is to say, the controller action can define the named parameter. the parameter value will be automatically filled by Yii from $ _ GET.

To describe this function in detail, assume that we need to write a create action for PostController. This action requires two parameters to be passed through $ _ GET:

Category: an integer that represents the ID of the category in which the post is to be published.

Language: a string that represents the language code used by the Post.

When extracting parameters from $ _ GET, we can no longer write the relevant verification code as follows:

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

Now, with the action parameter function, we can easily complete tasks such as the above code:

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

Note that two parameters are added to actionCreate. The names of these parameters must be the same as the names we want to extract from $ _ GET. When the $ language parameter is not specified in the request, the default value en is used for this parameter. Because $ category has no default value, if you do not provide the category parameter in $ _ GET, a CHttpException (error code 400) exception is automatically thrown.

Yii supports array action parameters starting from version 1.1.5. The usage is as follows:

class PostController extends CController{public function actionCreate(array $categories){// Yii will make sure $categories be an array}}
Articles you may be interested in
  • When CuteFTP is connected to the ftp server, the "invalid parameters" error is displayed.
  • Linux chmod (file or folder permission setting) command parameters and usage details
  • Summary of system constants in the Action Controller of thinkphp
  • Invincible Firewheel effect in the address bar of JavaScript browser
  • JQuery-based left-right scrolling and automatic scrolling
  • Windows cannot start the hardware device because its configuration information (in the registry) is incomplete or damaged. (Code 19) solution
  • How to obtain the complete url address and parameters in javascript
  • PHP checks browser parameters to prevent SQL injection.

Starting from version 1.1.4, Yii provides support for binding automatic action parameters. That is to say, controller actions can define named parameters, parameter values...

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.