书籍详情页面

    在该页面中我们要显示很多东西:书籍的所有登陆信息,包括TAG,一个jQuery的表单方便访客增加自己的TAG,如果这本书有相关的评论,那么还要显示评论的连接等等等等。该页面编程量非常大,请做好准备。

    这个页面很长,我这里就不贴出全部的代码了。同时我也不会写出所有的步骤:一般而言,SF给出的错误提示是非常精确的。在过程中参考错误提示,不断地修订——增加路由,控制器方法,模板等,最终一定能调试通过。

    我们贴一张最终渲染的效果图:

    我们分别来看几个重要的版块。

    显示书籍封面图片

    我们这里要显示的书籍封面图片,以JPG图片格式保存在目录之下,每本书都对应一个图片,其格式是{{bookid}}.jpg,例如途中所示的书其bookid99999,那么它对应的封面图片应该是99999.jpg。如果该文件名的图片不存在,我们将显示一个default.jpg

    我们先看其路由定义:

    从路由定义我们可以看到,我们的图片是动态生成的。

    再看对应的动作,我们将其放置在DefaultController中的cover动作中:

    1. public function coverAction($id, $title, $author, $width)
    2. {
    3. // Construct image file name based on
    4. $path = 'covers/';
    5. $ext = '.jpg';
    6. $filename = $path . $id . $ext;
    7. $default = false;
    8. // Check if the file exists
    9. if (!file_exists($filename))
    10. {
    11. $filename = $path . 'default' . $ext;
    12. $default = true;
    13. }
    14. list($w, $h) = getimagesize($filename);
    15. $nw = 300;
    16. $nh = $nw / $w * $h; // Propotionally change the width/height
    17. // Resize image
    18. $oimg = imagecreatefromjpeg($filename);
    19. $nimg = imagecreatetruecolor($nw, $nh);
    20. imagecopyresampled($nimg, $oimg, 0, 0, 0, 0, $nw, $nh, $w, $h);
    21. // Print copyright texts
    22. $copytext1 = "任氏有无轩";
    23. $copytext2 = "版权所有";
    24. $color = imagecolorallocate($nimg, 255, 255, 255);
    25. $color2 = imagecolorallocate($nimg, 0, 0, 0);
    26. $font = $path . 'msyh.ttf';
    27. imagettftext($nimg, 10, 0, 10, 26, $color, $font, $copytext1);
    28. imagettftext($nimg, 10, 0, 10, 40, $color, $font, $copytext2);
    29. imagettftext($nimg, 10, 0, 10, 54, $color, $font, $copytext3);
    30. if ($default)
    31. {
    32. //Print title
    33. imagettftext($nimg, 12, 0, 10, 140, $color2, $font, $title);
    34. // Print author
    35. imagettftext($nimg, 24, 0, 10, 240, $color, $font, $author);
    36. }
    37. //Resize the image to fit into reading list
    38. if ($width <> 300) //300 is the image width for book detail
    39. {
    40. $height = $width / $nw * $nh;
    41. $timg = imagecreatetruecolor($width, $height);
    42. imagecopyresampled($timg, $nimg, 0, 0, 0, 0, $width, $height, $nw, $nh);
    43. }
    44. // Output the image
    45. header('Content-type: image/png');
    46. if ($width == 300)
    47. {
    48. imagepng($nimg, null, 9);
    49. }
    50. else
    51. {
    52. imagepng($timg, null, 9);
    53. }
    54. imagedestroy($timg);
    55. }

    这段代码比较长,用到了PHP的GD扩展对图片进行操作。但是流程还是比较直观,请读者自行分析。

    注意:图片处理过程中要用到字体文件。由于字体文件比较大,作者没有将其放入代码仓库中,请读者自行拷贝。

    显示TAG的过程比较简单,直接贴代码:

    1. <small>{{ render (controller('AppBundle:Book:tagsbyid', {"id":book.id})) }}</small>

    显示完书籍的TAG后,我们显示一个按钮,让用户能添加自己的TAG:

    这是标准jQuery的模态对话框。我们不进行特别的分析。只是提请大家注意表单的编写方式。

    获取豆瓣信息

    我们希望从第三方(豆瓣)获取书籍的一些信息(TAG、介绍、评分等)。这里我们要用到已经用了很多次的file_get_contents函数:

    1. public function detailAction($id, Request $request)
    2. {
    3. $logger = $this->createLogger();
    4. $books = json_decode(file_get_contents("http://api/book/bookByBookId/$id"))->out;
    5. if (count($books) == 0) // Book not found
    6. {
    7. return $this->render("AppBundle:book:BookNotFound.html.twig", array('bookid' => $id));
    8. }
    9. $book = $books[0];
    10. $isbn = $book->isbn;
    11. $douban = json_decode(file_get_contents("http://api/douban/douban/$isbn"))->out;
    12. if ($douban->summary == '(豆瓣找不到)')
    13. {
    14. $logger->addError('豆瓣找不到ISBN ' . $book->isbn . '的书:' . $book->title);
    15. }
    16. else
    17. {
    18. $logger->addInfo('豆瓣找到ISBN ' . $book->isbn . '的书:' . $book->title);
    19. }
    20. $lvt = json_decode(file_get_contents("http://api/book/lastvisit/" . $book->bookid))->out;
    21. $session = $request->getSession();
    22. $count = $session->get('addvc', 1);
    23. $session->remove('addvc');
    24. $vc = json_decode(file_get_contents("http://api/book/visitCount/" . $book->id . "," . $count))->out;
    25. return $this->render("AppBundle:book:detail.html.twig", array('book' => $book, 'douban' => $douban, 'vc' => $vc, 'lvt' => $lvt));

    注意到我们此时用的是传递变量到模板的方式。但是方法本身全部采用RESTful API调用的方式。

    基于我们的样本数据,这本书应该有2个评论文章。所以我们还要获取相应的评论并显示。该段代码比较简单,请读者自行完成。

    小结

    至此,我们已经基本完成书籍详情页面的编写。