[英]Symfony2 Google Books API
我在Symfony2中從Google圖書API獲取圖書時遇到問題
我正在將此表單發布到頁面(/ google)...
<form action="/googlevolume" method="post">
<input type="text" name="title" id="title" size="40" value="">
<input type="submit" value="Search">
</form>
這是我對結果頁面(/ 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));
}
我嘗試過從表單中發布此號碼
1781100489
與轉到以下網址相同: https : //www.googleapis.com/books/v1/volumes?q = 1781100489
但是,當我在表格中輸入該數字並按搜索時,出現此錯誤
Controller "Blogger\BlogBundle\Controller\PageController::googlevolumeAction" for URI "/googlevolume" is not callable.
這是從我的路由文件...
google:
pattern: /google
defaults: { _controller: BloggerBlogBundle:Page:google }
requirements:
_method: GET
googlevolume:
pattern: /googlevolume
defaults: { _controller: BloggerBlogBundle:Page:googlevolume }
requirements:
_method: POST
這是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 %}
任何人都有任何想法,我要去哪里錯了?
謝謝
看起來當您實際上只是想從表單中獲取值時,您正在嘗試序列化請求對象。 嘗試將您對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);
}
然后的路線:
googlevolume:
pattern: /googlevolume
defaults: { _controller: BloggerBlogBundle:Page:googlevolumeAction }
requirements:
_method: POST
然后清除緩存:
php app/console cache:clear --env=prod
或僅用於開發
php app/console cache:clear
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.