Skip to content

Commit

Permalink
Add JSON view for packets
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 21, 2019
1 parent 24ac9dd commit 6671171
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ function main() {
$router->addRoute( // URL: /news/delete
"#^/news/delete/?$#", "News\\Delete", "News\\DeleteHtml"
);
$router->addRoute( // URL: /packet/:id.json
"#^/packet/(\d+)/?.*\.json$#", "Packet\\View", "Packet\\ViewJSON"
);
$router->addRoute( // URL: /packet/:id.txt
"#^/packet/(\d+)\.txt#", "Packet\\View", "Packet\\ViewPlain"
);
Expand Down
25 changes: 25 additions & 0 deletions src/views/Packet/ViewJSON.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace BNETDocs\Views\Packet;

use \BNETDocs\Models\Packet\View as PacketViewModel;

use \CarlBennett\MVC\Libraries\Common;
use \CarlBennett\MVC\Libraries\Exceptions\IncorrectModelException;
use \CarlBennett\MVC\Libraries\Model;
use \CarlBennett\MVC\Libraries\View;

class ViewJSON extends View {

public function getMimeType() {
return 'application/json;charset=utf-8';
}

public function render( Model &$model ) {
if ( !$model instanceof PacketViewModel ) {
throw new IncorrectModelException();
}
echo json_encode( $model->packet, Common::prettyJSONIfBrowser() );
}

}

0 comments on commit 6671171

Please sign in to comment.