Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding JSON, XML and text types to paste view output.

Adding the proper extension to the URL (.json, .xml or .txt) will return
the paste in the specified format.
  • Loading branch information...
commit 4bdff54df89ef43706f4e08fe7382ed5f11aaa97 1 parent 416234e
Joel Perras jperras authored
11 config/bootstrap.php
View
@@ -85,9 +85,14 @@
* return $posts->to('json');
* }}}
*/
-// use \lithium\util\Collection;
-//
-// Collection::formats('\lithium\net\http\Media');
+use \lithium\util\Collection;
+
+Collection::formats('\lithium\net\http\Media');
+
+/**
+ * Include Media handling of addtional render types.
+ */
+require __DIR__ . '/bootstrap/media.php';
?>
17 config/bootstrap/media.php
View
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+use \lithium\net\http\Media;
+
+$default = Media::type('default');
+$text = Media::type('text');
+
+Media::type('xml', null, $default['options']);
+Media::type('txt', null, $text['options']);
+
+?>
2  config/routes.php
View
@@ -20,6 +20,8 @@
Router::connect('/add/{:args}', array('controller' => 'pastes', 'action' => 'add'));
Router::connect('/edit/{:args}', array('controller' => 'pastes', 'action' => 'edit'));
+
+Router::connect('/view/{:args}.{:type}', array('controller' => 'pastes', 'action' => 'view'));
Router::connect('/view/{:args}', array('controller' => 'pastes', 'action' => 'view'));
Router::connect('/test/{:args}', array('controller' => '\lithium\test\Controller'));
7 controllers/PastesController.php
View
@@ -61,6 +61,13 @@ public function view($id = null) {
if ($paste->rewind() == 'not_found') {
$this->redirect(array('controller' => 'pastes', 'action' => 'index'));
}
+
+ if ($this->request->type === 'json') {
+ return $paste->to('json');
+ }
+ if ($this->request->type === 'txt') {
+ return $paste->content;
+ }
return compact('paste');
}
2  views/layouts/default.xml.php
View
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<?php echo $this->content; ?>
1  views/pastes/view.txt.php
View
@@ -0,0 +1 @@
+<?php echo 'here'; ?>
9 views/pastes/view.xml.php
View
@@ -0,0 +1,9 @@
+<paste>
+ <id><?=$paste->id; ?></id>
+<?php if ($paste->author): ?>
+ <author><?=$paste->author; ?></author>
+<?php endif; ?>
+ <created><?=$paste->created; ?></created>
+ <language><?=$paste->language; ?></language>
+ <content><?=$paste->content; ?></content>
+</paste>
Please sign in to comment.
Something went wrong with that request. Please try again.