Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
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
@jperras jperras authored
View
11 config/bootstrap.php
@@ -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';
?>
View
17 config/bootstrap/media.php
@@ -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']);
+
+?>
View
2  config/routes.php
@@ -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'));
View
7 controllers/PastesController.php
@@ -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');
}
View
2  views/layouts/default.xml.php
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<?php echo $this->content; ?>
View
1  views/pastes/view.txt.php
@@ -0,0 +1 @@
+<?php echo 'here'; ?>
View
9 views/pastes/view.xml.php
@@ -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>

0 comments on commit 4bdff54

Please sign in to comment.
Something went wrong with that request. Please try again.