CakePHP Dynamic QR Code

With the latest technology compacted to smart phone, the used of Quick Respond Code a.k.a QR Code has been practically useful for fast information retrieval or access. Preparing the QR code for CakePHP is easy as Google already share the Application Programming Interface (API) for QR code generator in any system. A helper for QR Code is necessary as this helper logic can be share between many views, elements, or layouts.

Create new file QrHelper.php inside …app/View/Helper. The file should contain the following codes:

class QrHelper extends AppHelper {
var $helpers=array('Html');
var $size='150x150';//QR Code size
var $encode='UTF-8';
* Error correction level
* L - [Default] Allows recovery of up to 7% data loss
* M - Allows recovery of up to 15% data loss
* Q - Allows recovery of up to 25% data loss
* H - Allows recovery of up to 30% data loss
var $error_correction='L';
var $margin=4;//margin around chart data portion
//Google API QR Code Generation 
var $base_url='';

//Text encoder
function text($text='',$options=array()){
return $this->Html->image($this->base_url . urlencode($text).$this->_optionsString($options));

//URL encoder
function url($url='',$options=array()){
$url= Router::url($url,true);
return $this->Html->image($this->base_url . urlencode($url).$this->_optionsString($options));

//Email encoder
function email($mail='',$options=array()){
return $this->Html->image($this->base_url . urlencode('mailto:'.$mail).$this->_optionsString($options));
//Phone encoder
function telephone($phone='',$options=array()){
return $this->Html->image($this->base_url . urlencode('tel:'.$phone).$this->_optionsString($options));

//Contact encoder
function contact($contact=array(),$options=array()){
$url=$this->base_url . urlencode($ret).$this->_optionsString($options);
return $this->Html->image($url);

//Send a SMS to a given number
function sms($number='',$options=array()){
return $this->Html->image($this->base_url . urlencode('sms:'.$number).$this->_optionsString($options));

//Send a MMS to a given number
function mms($number='',$options=array()){
return $this->Html->image($this->base_url . urlencode('mms:'.$number).$this->_optionsString($options));
//Geocode encoder
function geo($geo=array(),$options=array()){
return $this->Html->image($this->base_url . urlencode('geo:'.$geo['lat'].','.$geo['lon'].','.$geo['height']).$this->_optionsString($options));
//Android Play store search encoder
function market($app='',$options=array()){
return $this->Html->image($this->base_url . urlencode('market://search?q='.$app).$this->_optionsString($options));

//Merges all options array and returns as url parameter string
function _optionsString($options){

QR Code is not a surrogate record which can represent the whole information as it is limited to certain amount of character. The most effective method to provide the information for the users is by create a QR Code that represent the URL of the information itself. By sharing the URL, users can directly open the information through their other devices such as smart phone or tablet. To generate dynamic QR based on record id, developer need to add the following code to your view page. The QR need to refer to the full URL with the hostname of the system to generate the QR and the code to make it possible is shown below:

<?php echo $this->Qr->text(Router::url($this->here,true)); ?>

Developer also can dynamically create the QR based on the attributes entity eg:

<?php echo $this->Qr->text($user['User']['fullname']);?>

*The QR Code is generate using API, developer must have internet connection. Happy coding!