1
我想在操作頁面中回顯某些內容。如何在不顯示YII2內容的情況下回顯內容
可以說我想在yii佈局中回顯hello word
。
什麼,我知道是字寫招呼到一個文件(hello.php在視圖中),並使用$this->render('hello');
所以如何使它更短,像$this->echo('hello word');
,所以誼會顯示hello word
內部佈局?
我想在操作頁面中回顯某些內容。如何在不顯示YII2內容的情況下回顯內容
可以說我想在yii佈局中回顯hello word
。
什麼,我知道是字寫招呼到一個文件(hello.php在視圖中),並使用$this->render('hello');
所以如何使它更短,像$this->echo('hello word');
,所以誼會顯示hello word
內部佈局?
呈現HTML視圖不是必需的。這些行動2應輸出消息:
public function actionHello()
{
echo 'hello word !';
}
public function actionHello2()
{
return 'hello word !';
}
其實內置yii\rest\Controller
及其用於REST API孩子ActiveController
正在返回的數據是在第二個例子actionHello2()
做了同樣的方式。除了他們使用的是ContentNegotiator過濾器輸出JSON和/或XML,而不是純文本:
'contentNegotiator' => [
'class' => \yii\filters\ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
'application/xml' => Response::FORMAT_XML,
],
],
如果你需要的動作來呈現視圖,而不主要佈局可以使用renderPartial:
public function actionAbout()
{
echo 'hello word !';
return $this->renderPartial('about');
}
如果您需要的是使主要佈局和輸出數據,而無需渲染動作視圖,您可以使用renderContent:
public function actionHello()
{
return $this->renderContent('hello word !');
}
更多的渲染選項也可以在相關的docs中找到。
我更新了答案。我認爲'$ this-> renderContent('hello word')'是你想要在主佈局中輸出消息但不顯示動作文件的那個。 –