0
A
回答
2
有幾種方法來實現這一目標:
1實現一個afterExecuteRoute
事件
class MyController extends Phalcon\Mvc\Controller
{
public function showAsXml1Action()
{
return "<root><key>k</key></root";
}
public function showAsXml2Action()
{
return "<root><key>k</key></root";
}
public function afterExecuteRoute($dispatcher)
{
$response = new Phalcon\Http\Response();
$response->setHeader('Content-Type', 'application/xml');
$response->setContent($dispatcher->getReturnedValue());
$dispatcher->setReturnedValue($response);
}
}
2創建一個處理XML響應類:
class MyXMLResponse extends Phalcon\Http\Response
{
public function __construct($xml)
{
$this->setHeader('Content-Type', 'application/xml');
$this->setContent($xml);
}
}
在控制器:
public function showAsXml2Action()
{
return MyXmlResponse("<root><key>k</key></root");
}
3創建處理XML響應的插件:
class MyController extends Phalcon\Mvc\Controller
{
public function showAsXml1Action()
{
return array(
"type" => "xml",
"content" => "<root><key>k</key></root",
);
}
}
插件:
class ContentTypePlugin extends \Phalcon\Mvc\User\Plugin
{
public function afterExecuteRoute($event, $dispatcher)
{
$content = $dispatcher->getReturnedValue();
switch ($content['type']) {
case 'xml':
$response = new Phalcon\Http\Response();
$response->setHeader('Content-Type', 'application/xml');
$response->setContent($content['content']);
$dispatcher->setReturnedValue($response);
break;
}
}
}
使用註釋
/** * @Response(type="xml") */ public function showAction() { return "<root><key>k</key></root"; }
插件:
class AnnotationsContentPlugin extends \Phalcon\Mvc\User\Plugin
{
public function afterExecuteRoute($event, $dispatcher)
{
$annotations = $this->annotations->getMethod(
$dispatcher->getActiveController(),
$dispatcher->getActiveMethod()
);
// Check if the method has an annotation 'Response'
if ($annotations->has('Response')) {
// Get the type
$type = $annotations->get('Response')
->getNamedParameter('type');
if ($type == 'xml') {
$response = new Phalcon\Http\Response();
$response->setHeader('Content-Type', 'application/xml');
$response->setContent($dispatcher->getReturnedValue());
$dispatcher->setReturnedValue($response);
}
}
}
}
如果你需要使用的文件層次結構,如XML輸出:
app/views/index.phtml
app/views/layouts/posts.phtml
app/views/posts/show.phtml
您可以使用下面的代碼在動作
public function showAction()
{
$this->view->setRenderLevel(Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
$this->view->xml = '<root><key>k</key></root>';
}
,並在視圖:
<?php echo $xml; ?>
相關問題
- 1. MVC:爲所有操作添加輸出緩存
- 2. phalcon伏特按位操作?
- 3. tlibimp.exe輸出在不同的操作系統上有所不同
- 4. 作爲cron作業的控制器操作phalcon
- 5. 輸入/輸出操作 - ML
- 6. Python 3.x - 執行操作所需的輸出時間爲DD:HH:MM:SS.SSS
- 7. 在c中操作輸出字符串#
- 8. 在mathematica中調整操作輸出
- 9. 製作XML輸出
- 10. 如何在xls中輸出所有的xml?
- 11. 沒有在Java中獲得所需的XML輸出
- 12. 如何在JavaScript中輸出xml字符串中的所有perticuler xml元素值?
- 13. Phalcon Debug不會執行任何操作
- 14. 俘獲輸出爲空SSH操作
- 15. 是否所有的RPN表達式都可以表示爲所有的操作符出現在左側,而所有的操作數出現在右側?
- 16. 輸出圖爲`.xml`
- 17. 所需輸出所需的數據庫操作
- 18. 爲什麼取消所有操作不會刪除所有操作?
- 19. 爲所有操作分配bean還是爲所有bean分配?
- 20. 如何從json輸出中獲取所有值作爲keyname = keyvalue?
- 21. 在PHP中操作XML
- 22. XML操作在PHP中
- 23. MySQL中內置了「出現在所有」(set division?)操作嗎?
- 24. 所有的輸出結果工作,但tableview中沒有在iphone
- 25. Struts 2從xml中獲取所有命名空間和操作
- 26. XML ::在輸出
- 27. nslookup的輸出操作
- 28. 重載輸出操作者
- 29. 用sed操作df輸出
- 30. 操作數據庫輸出