[英]Symfony2 Google Books API
I have an issue getting a book from the Google Books API in Symfony2 我在Symfony2中从Google图书API获取图书时遇到问题
I am posting this form for a page (/google)... 我正在将此表单发布到页面(/ google)...
<form action="/googlevolume" method="post">
<input type="text" name="title" id="title" size="40" value="">
<input type="submit" value="Search">
</form>
and this is my controller for the result page (/googlevolume)... 这是我对结果页面(/ googlevolume)的控制...
public function googlevolume(Request $request)
{
$enquiry = new Enquiry();
$form->bind($request);
$response = $enquiry->get("https://www.googleapis.com/books/v1/volumes?q=".$form->get('title')->getData());
$data=$response->json();
$response2=$data['items'];
return $this->render('BloggerBlogBundle:Page:googlevolume.html.twig', array('items' => $response2));
}
I have tried posting this number from the form 我尝试过从表单中发布此号码
1781100489
Which is the same as going to: https://www.googleapis.com/books/v1/volumes?q=1781100489 与转到以下网址相同: https : //www.googleapis.com/books/v1/volumes?q = 1781100489
However, when i put that number in the form and press search, i get this error 但是,当我在表格中输入该数字并按搜索时,出现此错误
Controller "Blogger\BlogBundle\Controller\PageController::googlevolumeAction" for URI "/googlevolume" is not callable.
this is from my routing file... 这是从我的路由文件...
google:
pattern: /google
defaults: { _controller: BloggerBlogBundle:Page:google }
requirements:
_method: GET
googlevolume:
pattern: /googlevolume
defaults: { _controller: BloggerBlogBundle:Page:googlevolume }
requirements:
_method: POST
this is googlevolume.html.twig ... 这是googlevolume.html.twig ...
{# src/Blogger/BlogBundle/Resources/views/Page/googlevolume.html.twig #}
{% extends 'BloggerBlogBundle::layout.html.twig' %}
{% block title %} Google Books{% endblock%}
{% block body %}
<header>
<h1>Google Book</h1>
</header>
<br>
{% for item in items %}
<article>
<img src="{{ item.volumeInfo.imageLinks.thumbnail}}"/>
<h4>{{ item.volumeInfo.title}}</h4>
{% if item.volumeInfo.description is defined %}
{{ item.volumeInfo.description }}
{% endif %}
<strong> {{ item.volumeInfo.publishedDate }}</strong><br/>
<b>{{ item.volumeInfo.authors | join }}</b>
</article>
{% endblock %}
Anyone got any ideas where i'm going wrong with this? 任何人都有任何想法,我要去哪里错了?
Thanks 谢谢
It looks like you are trying to serialize the request object when really you just want the value from the form. 看起来当您实际上只是想从表单中获取值时,您正在尝试序列化请求对象。 Try changing your request to the api to: 尝试将您对api的请求更改为:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
...
public function googlevolumeAction(Request $request)
{
$form = $this->createFormBuilder(null,[
'csrf_protection' => false
])
->add('title','text')
->add('Search', 'submit')
->getForm();
$form->bind($request);
if($form->isValid()){
$enquiry = new Enquiry();
$response = json_decode(file_get_contents("https://www.googleapis.com/books/v1/volumes?q=".$form->get('title')->getData()),true);
if(array_key_exists('items',$response)){
return $this->render('BloggerBlogBundle:Page:googlevolume.html.twig', [
'items' => $response['items']
]);
} else {
return new Response('Google Volume did not have any items', 400);
}
}
return new Response('Google Volume Not Found', 404);
}
Then the route: 然后的路线:
googlevolume:
pattern: /googlevolume
defaults: { _controller: BloggerBlogBundle:Page:googlevolumeAction }
requirements:
_method: POST
Then clear the cache: 然后清除缓存:
php app/console cache:clear --env=prod
or just for dev 或仅用于开发
php app/console cache:clear
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.