entomb / slim-json-api Goto Github PK
View Code? Open in Web Editor NEWSlim extension to implement fast JSON API's
License: MIT License
Slim extension to implement fast JSON API's
License: MIT License
Hey, thanks for the great little api. I just encountered a problem when using Slim's etags... when it is trying to return the 304 Not Modified, slim-json-api picks up the empty response body and sets an error.
I made a quick fix at line 72 of jsonApiMiddleware.php:
if ($app->response()->getStatus() != 304 && strlen($app->response()->body()) == 0) {
Hi,
When I call the view object from my own middleware class, the following error is called.
PHP Fatal error: Call to a member function getMessages() on a non-object in /Users/ben/www/collateit/mvp/api/vendor/entomb/slim-json-api/jsonAPI/JsonApiView.php on line 42
which relates to this line:
$flash = $this->data->flash->getMessages();
presumably 'flash' is not an object?
Cheers
Ben
https://github.com/entomb/slim-json-api/blob/master/jsonAPI/JsonApiMiddleware.php#L33
I think setting debug value is developer choice
it seems we do not support either.
it will always try to output an empty array, not an empty object. if we set to use a data wrapper.
Replace :
$app->response()->body(json_encode($response));
with :
if (($jsonCallback = $app->request()->get('callback')) !== null) {
$response = sprintf("%s(%s);", $jsonCallback, json_encode($response));
$app->response()->body($response);
}else{
$app->response()->body(json_encode($response));
}
Please change jsonp "Content-Type" to "application/javascript"
public $contentTypeJson = 'application/json';
public $contentTypeJsonp = 'application/javascript';
if($jsonp_callback !== null){
$app->response()->header('Content-Type', $this->contentTypeJsonp);
$app->response()->body($jsonp_callback.'('.json_encode($response, $this->encodingOptions).')');
} else {
$app->response()->header('Content-Type', $this->contentTypeJson);
$app->response()->body(json_encode($response, $this->encodingOptions));
}
The JSON encoding of $this->data
doesn't work for the 2.3 branch of Slim. I've updated JsonApiView
to use the new data accessor functions in \Slim\View
. These functions are not backwards compatible, or I would've just made a pull request against master. You can find my code here.
Could we have a new release? :-)
I dislike defining non-absolute dependencies - like just tracking master of this repo...
When strict mode is enabled - slim shows such error
Declaration of JsonApiView::render() should be compatible with Slim\View::render($template, $data = NULL)
Hi,
i can't get the examples to work in slim 2.6. I used the "routing example". It seems the View is not set correctly. Slim throws an Exception .
Message: View cannot render `200` because the template does not exist
File: <<path>>\vendor\slim\slim\Slim\View.php
Can you confirm that it work with slim 2.6?
My Code:
function APIrequest(){
$app = \Slim\Slim::getInstance();
$app->config('debug', false);
$app->view(new \JsonApiView());
$app->add(new \JsonApiMiddleware());
}
$app->get('/api','APIrequest',function() use($app){
$app->render(200,array(
'msg' => 'welcome to my API!',
));
});
// Handle Empty response body
$app->hook('slim.after.router', function () use ($app) {
$res = $app->response();
if($res['Content-Type'] != 'application/octet-stream'){
if (strlen($app->response()->body()) == 0) {
$app->render(500,array(
'error' => TRUE,
'msg' => 'Empty response',
));
}
}
});
I added it to composer to my current slim app, added the Middleware and the view, but now I get this error returned?
{"error":true,"msg":"ERROR: could not find driver","status":500}
What gone wrong? Thanks in advance
Hey, thanks for your tidy little API. I just installed it into my app, but I am receiving this error when using the JsonApi middleware + view...
PHP Fatal error: Uncaught exception 'Slim\Exception\Stop' in /.../vendor/slim/slim/Slim/Slim.php:1017
Stack trace:
#0 /.../vendor/entomb/slim-json-api/jsonAPI/JsonApiView.php(86): Slim\Slim->stop()
I am calling the view render from another middleware's call() function as:
$this->app->render(401, array(
'error' => TRUE,
'message' => 'Access denied.'
));
Wondering why this happens and where would be the proper place to catch the exception?
Hi folks:
I'm sure I'm not the only one who's trying to implement a JSON API-compliant API using Slim and this (rather excellent) library.
As part of this spec, the Content-Type header should be set to application/vnd.api+json
rather than application/json
. There's a patch in the master branch since January which implements this tiny change but there's no release since August last year.
I've changed the composer.json
for my project to require dev-master
, but longer-term even a loose association with a release version would be better if possible. Any chance of a release at any stage? Cheers!
please support JSON API Specification, thanks
With the Slim 3.0 release candidate out, I've tried to get this to work but I'm getting
Fatal error: Class 'Slim\Middleware' not found in /var/www//vendor/entomb/slim-json-api/jsonAPI/JsonApiMiddleware.php on line 23
There have been some large changes to how middleware works, http://www.slimframework.com/docs/concepts/middleware.html
Will this package be updated to work with 3.0? I love it and want to keep using it
Fatal error: Class 'Slim\View' not found in C:\xampp\htdocs\slim\test\vendor\entomb\slim-json-api\jsonAPI\JsonApiView.php on line 24
Fatal error: Class 'Slim\Middleware' not found in C:\xampp\htdocs\slim\test\vendor\entomb\slim-json-api\jsonAPI\JsonApiMiddleware.php on line 23
Hello!
I have an issue with JsonApiView:
Declaration of JsonApiView::render() should be compatible with Slim\View::render($template, $data = NULL)
Installed via composer.
Error appears only if
ini_set ('error_reporting', E_ALL);
Test application:
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
require 'vendor/autoload.php';
use Slim\Slim;
use Slim\Middleware;
use Slim\Views\Twig;
$app = new Slim([
'view' => new Twig(),
'templates.path' => './public',
'mode' => 'development',
]);
$app->add(new Middleware\ContentTypes());
function APIrequest() {
$app = \Slim\Slim::getInstance();
$app->view(new \JsonApiView());
$app->add(new \JsonApiMiddleware());
}
$app->get('/api','APIrequest',function() use($app){
//this request will have full json responses
$app->render(200,array(
'msg' => 'welcome to my API!',
));
});
$app->run();
Loving the library! Just a quick point, when my webapp requests the available OPTIONS I have my code set a 200 response and an appropriate header:
Allow:OPTIONS,GET
However because there is no body, this library sets the header as a 500 and create an error. Which probably shouldnt happen. Should be a simple case to detect the response type and prevent this?
Cheers
Ben
I want rendering an array of object so i do :
$data = $sqlFetchArray;
$arr = (array) array();
foreach ($data as $t) {
$arr[] = (object) array('id' => $t[0], 'name' => $t[1]);
}
$app->render(200, $arr);
But i still get json object and no array of json object.
{
0: {id: 144, name: 'warlof'},
1: {id: 201, name: 'elfaus'}
}
instead of
[
{id: 144, name: 'warlof'},
{id: 201, name: 'elfaus'}
]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.