Ho configurato FOSRestBundle come segue:configurazione FOSRestBundle per JSON ritorno, ma ancora chiedendo template Twig
#FOSRestBundle
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener:
rules:
- { path: ^/, priorities: [ json, html ], fallback_format: ~, prefer_extension: true }
media_type:
version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'
body_converter:
enabled: true
validate: true
view:
mime_types:
json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
view_response_listener: 'force'
formats:
xml: false
json: true
templating_formats:
html: true
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
E ho questo al controllore:
namespace PDI\PDOneBundle\Controller\Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\Get;
class RepresentativeRestController extends FOSRestController
{
/**
* Get all representatives.
*
* @return array
*
* @ApiDoc(
* resource = true,
* https = true,
* description = "Get all representatives.",
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when errors"
* }
*)
* @Get("/api/v1/reps")
*/
public function getRepsAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('PDOneBundle:Representative')->findAll();
if(!$entities)
{
return $this->view(null, 400);
}
return $this->view($entities, 200);
}
}
Ma quando provo il seguente URL app_dev.php/api/v1/reps
ho ottenuto questo errore:
Unable to find template "". 500 Internal Server Error - InvalidArgumentException 3 linked Exceptions: Twig_Error_Loader » InvalidArgumentException » InvalidArgumentException »
mi aspetto che API restituiscono un ben formato JSON un s il seguente esempio:
{
"id":"30000001",
"veeva_rep_id":"0055648764067SwzAAE",
"display_name":"John Know",
"avatar_url":"http://freelanceme.net/Images/default%20profile%20picture.png",
"rep_type":"VEEVA",
"username":"[email protected]",
"first":"John",
"last":"Know",
"title":"Sales Representative",
"phone":"800-555-1212",
"email":"[email protected]",
"territory_id":"200454001",
"inactive":"no",
"total_contacts":"6",
"total_shares":"0",
"totalViews":"0",
"lastLoginAt":"2015-05-05 15:45:57",
"lastVeevaSyncAt":"2015-05-05 15:45:57",
"createdAt":"2015-05-05 15:45:57",
"updatedAt":"2015-05-05 15:45:57"
}
Non è configurato FOSRestBundle per restituire JSON? Perché chiedere ancora il modello Twig? Come posso risolvere questo?
Prima prova:
Come @Jeet mi suggeriscono Ho provato con Postman (è la stessa come l'estensione mi ha detto) e dopo impostare l'intestazione Content-Type
al application/json
l'errore si trasforma in questo
Malformed JSON
così, il FOSRestBundle non è la creazione di intestazioni come dovrebbe essere e il controller non è la restituzione di un JSON valida, come posso risolvere quelle?
Seconda prova:
Come suggerito da @Jeet ho eseguito questo test:
/**
* Get all representatives.
*
* @return array
*
* @ApiDoc(
* resource = true,
* https = true,
* description = "Get all representatives.",
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when errors"
* }
*)
* @Get("/api/v1/reps")
* @View()
*/
public function getRepsAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('PDOneBundle:Representative')->findAll();
$temp = array("1", "2", "3");
$view = $this->view($temp, Codes::HTTP_OK);
return $this->handleView($view);
}
E ancora lo stesso problema:
Unable to find template "". 500 Internal Server Error - InvalidArgumentException 3 linked Exceptions: Twig_Error_Loader » InvalidArgumentException » InvalidArgumentException »
Che altro può essere sbagliato Qui? Mi sono perso qualcosa durante la configurazione?
Ho dimenticato di aggiungere app/config/routing.yml
e src/PDI/PDOneBundle/Resources/config/routing.yml
in un primo momento ecco loro va, forse questo è il pezzo mancante del puzzle e vi darà una migliore idea di dove il problema proviene da:
#app/config/routing.yml
#PDOne
pdone:
resource: "@PDOneBundle/Resources/config/routing.yml"
template:
resource: "@TemplateBundle/Resources/config/routing.yml"
#FOSUserBundle
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix:/
#NelmioApiDocBundle:
NelmioApiDocBundle:
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /api/doc
#SonataAdmin
admin:
resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
#src/PDI/PDOneBundle/Resources/config/routing.yml
pdone:
resource: "@PDOneBundle/Controller/"
type: annotation
prefix: /
Terza prova :
Sicuramente qualcosa è sbagliato con la richiesta lato client, se uso uno strumento come Postman
e impostare le intestazioni appropriate ho ottenuto le entità come voglio, vedi foto qui sotto:
non riesco a trovare dove il problema è così ho disperatamente bisogno di aiuto di qualcuno qui perché ero già a corto di idee
Avete impostato "Content-Type" come "application/json"? – Jeet
@Jeet dove dovrei farlo? Non è nella configurazione dei FOSR come puoi vedere sull'OP? – ReynierPM
Prova app_dev.php/api/v1/ripetizioni nell'applicazione Advanced Rest Client di Google Chrome (estensione dell'app), qui avrai un'opzione. – Jeet