Turn: http://www.architecy.com/archives/361
There is a section in the sitecontroller. php file in the demo package of yii2:
Public Function actions ()
{
Return [
'Error' => [
'Class' => 'yii \ WEB \ erroraction ',
], // Error returned
'Captcha '=> [
'Class' => 'yii \ CAPTCHA \ captchaaction ',
'Fixedverifycode' => yii_env_test? 'Testme': NULL,
], // Return verification
];
}
In this program, two external actions, error and CAPTCHA, are used to display the error and verify the graphic Verification Code respectively.
(We can find the corresponding program file in Vendor/yiisoft/yii2 .)
Now let's take a look at the usage
In the browser, enter http: // localhost/index. php? R = site/CAPTCHA, a verification code is displayed;
In the browser, enter http: // localhost/index. php? R = site/error, an error prompt is displayed;
At this point, I believe you should have some knowledge about the use of actions. actions is the aggregation of a series of actions to call the use of each action.
Now let's write an independent action on our own.
Step 1: Create a New testaction. php, a self-written external action, which can be placed in any location. In this example, I put it in the frontend/actions of the website.
The code for testaction. php is as follows:
controller->render(‘test‘);
}
}
Step 2: Call testaction in testcontroller. The Code is as follows:
class TestController extends Controller{
public function actions()
{
return [
‘test‘=>[
‘class‘=>‘frontend\libs\TestAction‘,
]
];
}
Step 3: add the test. php View File under views/test
Independent action test
This is the demo page for testing actions!
Now, let's have a preliminary look.
In the browser, enter http: // localhost/index. php? R = test/test, the view code just written is displayed;
Next, click
Step 4: Modify testaction. PHP
class TestAction extends Action {
public $param1=NULL;
public $param2=NULL;
public function run($param=NULL) {
return $this->controller->render(‘test‘,[
‘param‘=>$param,
‘param1‘=>$this->param1,
‘param2‘=>$this->param2
]);
}
}
You can see that there is a parameter Param that can be passed through the browser, and the other two are configurable parameters.
Step 5: Modify the View File views/test. php:
Testaction
This is the testaction demo page!
$ Param = ""
$ Param1 = ""
$ Param2 = ""
Enter http: // localhost/index. php In the browser? R = test/test using M = actiontest, the modified view code is displayed.
All the independent actions are done once. If you want to modify the default error effect, you can find the corresponding file.
Finally, yii2 provides the following system-level actions for direct use:
Inlineaction
Captchaaction
Createaction
Deleteaction
Indexaction
Optionsaction
Updateaction
Viewaction
Erroraction
Generateaction
Yii2 built-in independent operations