Commit 2081c6f4 by Mark

subsection added

parent b079c73a
...@@ -35,28 +35,7 @@ class SiteController extends Controller ...@@ -35,28 +35,7 @@ class SiteController extends Controller
``` ```
As you can see, typical controller contains actions that are public class methods named as `actionSomething`. As you can see, typical controller contains actions that are public class methods named as `actionSomething`.
The output of an action is what the method returns, it could be rendered result or it can be instance of ```yii\web\Response```, for example: The output of an action is what the method returns, it could be rendered result or it can be instance of ```yii\web\Response```, for [example](#custom-response-class).
```php
namespace app\controllers;
use yii\web\Controller;
use app\components\web\MyCustomResponse; #extended from yii\web\Response
class SiteController extends Controller
{
public function actionCustom()
{
/*
* do your things here
* since Response in extended from yii\base\Object, you can initialize its values by passing in
* __constructor() simple array.
*/
return new MyCustomResponse(['data' => $myCustomData]);
}
}
```
The return value will be handled by the `response` application The return value will be handled by the `response` application
component which can convert the output to differnet formats such as JSON for example. The default behavior component which can convert the output to differnet formats such as JSON for example. The default behavior
is to output the value unchanged though. is to output the value unchanged though.
...@@ -271,4 +250,27 @@ TBD ...@@ -271,4 +250,27 @@ TBD
See also See also
-------- --------
Custom response class
---------------------
```php
namespace app\controllers;
use yii\web\Controller;
use app\components\web\MyCustomResponse; #extended from yii\web\Response
class SiteController extends Controller
{
public function actionCustom()
{
/*
* do your things here
* since Response in extended from yii\base\Object, you can initialize its values by passing in
* __constructor() simple array.
*/
return new MyCustomResponse(['data' => $myCustomData]);
}
}
```
- [Console](console.md) - [Console](console.md)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment