《创建symfony插件》一文中对如何编写插件进行了简单的介绍, 下面用一个例子来详细讲解一些如何创建一个生成QRcode的插件.
什么是QR code(QR码)?
简单来说, QR code是用来存储数据的矩形黑白点阵, 可以在很小的图片中存储大量的数据, 在移动领域应用广泛. 详细的说明请参考维基百科(http://zh.wikipedia.org/wiki/QR%E7%A2%BC)
应项目需求, 我们要编写一个用来生成QR code的Helper, 为了更好的重用性, 我们要将这个Helper写成一个插件.
首先, 创建目录结构
$cd plugins$mkdir QRcodePlugin$cd QRcodePlugin$mkdir lib web$mkdir lib/helper
生成QR code的过程中我们使用到了一个第三方类库 qr_img0.50g, 在readme中我们找到了使用方法如下:
php/qr_img.php?d=data[&e=(L,M,Q,H)][&s=int size][&v=(1-40)][&t=J]
[&m=(1-16)&n=(2-16)[&o=original data][&p=(0-255)]]
访问该文件会根据url中的参数生成一个QR code 图片, 我们只要将这个库放到web目录下就可以了.
$mv qr_img0.50g pathtoproject/plugins/QRcodePlugin/web/qr_img
然后我们来编写Helper, 根据symfony中helper的命名规则创建文件如下
$cd pathtoproject/plugins/QRcodePlugin/lib/helper
$vi QRcodeHelper.php
<?php
# [useage]
# qr_img.php?d=[data]&e=[(L,M,Q,H)]&s=[int]&v=[(1-40)]
# (&m=[(1-16)]&n=[(2-16)](&p=[(0-255)],&o=[data]))
#
# d= data URL encoded data.
# e= ECC level L or M or Q or H (default M)
# s= module size (dafault PNG:4 JPEG:8)
# v= version 1-40 or Auto select if you do not set.
# t= image type J:jpeg image , other: PNG image
#
# structured append m of n (experimental)
# n= structure append n (2-16)
# m= structure append m (1-16)
# p= parity
# o= original data (URL encoded data) for calculating parity
#
function QRcode($d, $e = 'M', $s = null, $v = null, $t = 'P', $m = null, $n = null, $o = null, $p = null)
{
$path = '/QRcodePlugin/qr_img/php/qr_img.php';
$url = $path . '?d=' . $d;
if ($e && in_array(strtoupper($e), array('L', 'M', 'Q', 'H'))) {
$url .= '&e=' . strtoupper($e);
} else {
$url .= '&e=M';
}
if ($s) {
$url .= '&s=' . $s;
}
if ($v) {
$url .= '&v=' . $v;
}
if ($t) {
$url .= '&t=' . $t;
}
if ($m) {
$url .= '&m=' . $m;
}
if ($n) {
$url .= '&n=' . $n;
}
if ($o) {
$url .= '&o=' . $o;
}
if ($p) {
$url .= '&p=' . $p;
}
$output = image_tag($url, array('alt' => $d));
return $output;
}
这样, 我们就写好了一个只有一个Helper的Plugin, 我们来看一下插件结构如下.
|--.lib
|...`--.helper
|.......`--.QRcodeHelper.php
`--.web
....`--.qr_img
........|--.data
........|--.image
........`--.php
............`--.qr_img.php《创建symfony插件》一文中我们说过项目中是无法直接访问plugin中的web目录的, 所以我们还要copy一份web目录到项目的web目录下
$cp -rf pathtoproject/plugins/QRcodePlugin/web pathtoproject/web/QRcodePlugin接下来我们来测试一下Plugin是否有效.
$cd pathtoproject/$./symfony init-app test$./symfony init-module test test$vi apps/test/modules/test/actions/actions.class.php<?php
class testActions extends sfActions
{
public function executeIndex(sfWebRequest $request) { }
}
$vi apps/test/modules/test/templates/indexSuccess.php<?php use_helper('QRcode');?>
<?php echo QRcode('fuck');?>
访问http://project/test.php/test/index效果如下:

源代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" media="screen" href="/css/main.css" />
</head>
<body>
<img alt="fuck" src="/QRcodePlugin/qr_img/php/qr_img.php?d=fuck&t=P" />
</body>
</html>
测试通过, 本文完.