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