Back to Blog
Dotkernel

Using DotKernel with Composer Dependencies

This article will cover the external dependency usage VIA composer within DotKernel applications. There is also an article explaining how composer can be added to DotKernel learn more. Composer automatically loads our dependencies so there is no need to include/require them. For this example we will render a Barcode using Zend Framework 1 as the Non-Namespaced dependency and Zend Framework 2 as the Namespaced dependency.

The Composer Dependencies

ZendFramework 1 Barcode module can only be loaded with the ZF1 itself:
>composer require 'zendframework/zendframework1'
The ZendFramework 2 Barcode module can be separately loaded:
>composer require 'zendframework/zend-barcode'
 

Important note

These dependencies can be used anywhere after the Dot_Kernel::initialize() function was called
 

Using Non-Namespaced Dependencies (Zend Framework 1)

The class is loaded PSR-0 style, meaning the class name looks like VendorName_PackageName_ClassName
>// Only the text to draw is required
$barcodeOptions = array('text' =>; 'ZEND-FRAMEWORK');
 
// No required options
$rendererOptions = array();
 
// Draw the barcode in a new image,
// send the headers and the image
Zend_Barcode::factory(
'code39', 'image', $barcodeOptions, $rendererOptions
)->render();
 

Using Namespaced Dependencies (Zend Framework 2)

The class is loaded PSR-4 style, meaning the class name looks like \VendorName\PackageName\ClassName
>use Zend\Barcode\Barcode;

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();

// Draw the barcode in a new image,
// send the headers and the image
Barcode::factory(
    'code39', 'image', $barcodeOptions, $rendererOptions
)->render();

The result

Both the examples will render the following barcode if nothing goes wrong: Resulting barcode. Source: http://framework.zend.com/images/manual/zend.barcode.introduction.example-1.png Resulting barcode.
Source: bit.ly/1XbZZ81

Tip

In this case the first example will work for both namespaced an non-namespaced dependencies if we add the the following as the first line at the first example:
>use Zend\Barcode\Barcode as Zend_Barcode;
Now we can use any of the following to access ZF2's Barcode Module:
  • Zend\Barcode\Barcode
  • Barcode
  • Zend_Barcode
    The full examples can be found here: Zend Framework 1 - Rendering a barcode Zend Framework 2 - Rendering a barcode This article works for any DotKernel 1.x version if your server is running PHP >5.4.0.