Skip to content

Commit a5b8bd7

Browse files
committed
Actions demo for Response class
1 parent b20827d commit a5b8bd7

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

controllers/ResponseController.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,60 @@
22

33
namespace app\controllers;
44

5+
use yii\helpers\Url;
6+
use yii\helpers\VarDumper;
7+
use yii\web\Response;
8+
59
class ResponseController extends \yii\web\Controller
610
{
711
public function actionIndex()
812
{
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+
1059
}
1160

1261
}

0 commit comments

Comments
 (0)