|
2 | 2 |
|
3 | 3 | namespace app\controllers; |
4 | 4 |
|
| 5 | +use yii\helpers\Url; |
| 6 | +use yii\helpers\VarDumper; |
| 7 | +use yii\web\Response; |
| 8 | + |
5 | 9 | class ResponseController extends \yii\web\Controller |
6 | 10 | { |
7 | 11 | public function actionIndex() |
8 | 12 | { |
9 | | - return $this->render('index'); |
| 13 | + return "Controller: {$this->getUniqueId()} \n Action Full: {$this->action->getUniqueId()} \n Action Only: {$this->action->id}"; |
| 14 | + } |
| 15 | + public function actionText() |
| 16 | + { |
| 17 | + /** |
| 18 | + * We don't have text/plain format in Response |
| 19 | + * so we set header |
| 20 | + */ |
| 21 | + $this->response->format = Response::FORMAT_RAW; |
| 22 | + $this->response->headers->add('Content-Type', 'text/plain; charset=utf-8'); |
| 23 | + return 'text/plain Response'; |
| 24 | + |
| 25 | + } |
| 26 | + public function actionJson() |
| 27 | + { |
| 28 | + $demoData = [ |
| 29 | + 'name' => 'Sergey', |
| 30 | + 'role'=> 'developer', |
| 31 | + 'skills' => ['PHP', 'Yii2', 'Node.js'] |
| 32 | + ]; |
| 33 | + return $this->asJson($demoData); |
| 34 | + } |
| 35 | + |
| 36 | + public function actionXml() |
| 37 | + { |
| 38 | + # Example @url - https://github.com/yiisoft/yii2/issues/14263#issuecomment-306780434 |
| 39 | + |
| 40 | + $this->response->format = Response::FORMAT_XML; |
| 41 | + $demoData = [ |
| 42 | + 'name' => 'Sergey', |
| 43 | + 'role'=> 'developer', |
| 44 | + 'skills' => ['PHP', 'Yii2', 'Node.js'] |
| 45 | + ]; |
| 46 | + return $this->asXML($demoData); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + public function actionRaw() |
| 51 | + { |
| 52 | + $this->response->format = Response::FORMAT_RAW; |
| 53 | + return 'RAW Response = html by default'; |
| 54 | + } |
| 55 | + public function actionStream() |
| 56 | + { |
| 57 | + # TODO: Stream Realisation |
| 58 | + |
10 | 59 | } |
11 | 60 |
|
12 | 61 | } |
0 commit comments