Showing posts with label Zend. Show all posts
Showing posts with label Zend. Show all posts

Thursday, October 10, 2013

Integrate and use zend framework 2 with codeigniter

Zend framework 2(ZF2) is an open source framework using PHP 5.3+ with more than 15 million downloads. It should be kept in mind that ZF2 is not backward compatible with ZF1. ZF2 has an unique component structure and each component only have few dependencies on other components. Simply Zend call this architecture as "use-at-will" design.


If you want to integrate Codeigniter and ZF2 then that's a good idea. I assume that you have downloaded both codeigniter and ZF2. Help on installing Zend Framework 2 in ubuntu can be found here.

Now as the two frameworks are downloaded, copy the Zend folder(under library) from Zend download to application/libraries in Codeigniter. The idea here is to use the Zend components as a library in Codeigniter. Lets see an example of using Zend Captcha in Codeigniter.

First we need to create a new file under application/libraries, lets call it "Zend". After creating this file and open it in a text editor and copy paste the following code:

Zend.php

require_once 'autoload.php';
class Zend{

public $obj;
public $objId;

public function __construct(){

}


public function createImageCaptcha($params=array()){
$this->obj = new Zend\Captcha\Image( array(
  'imgDir'=>'public/',
  'font' => 'font/opensans-regular-webfont.ttf',
  'width' => 250,
  'height' => 100,
  'dotNoiseLevel' => 40,
  'lineNoiseLevel' => 3)
);
$this->obj->generate();
return $this->obj;
}

}


In the constructor i am just including the corresponding class file from Zend. The function createImageCaptcha() actually creates an image of type png inside the path you specify, imgDir variable sets the file path and font variable sets the font path. Remember that the paths are relative your root folder. Here what i am trying to do is to create my own wrapper to the Zend components instead of calling them directly in controller. In my controller i would access my Zend Captcha as follows:

$this->load->library('zend');
$captcha = $this->zend->createImageCaptcha();


Here $captcha is an object and will hold all the information regarding the Captcha.  $captcha->getimgDir()  would give you the image directory, $captcha->getId() would give you the filename, $captcha->getsuffix() would give you the file extension and so on. All the properties can be accessed by just appending "get" to them. 

At last but not least we need to know about the file named "autoload.php" which is included in the top. The purpose of this file is to autoload all the dependency files in Zend while instantiating an object. If this file is not present then an error like the following may appear "Fatal error: Class 'Zend\Captcha\AbstractWord' not found", in case of Captcha component. Same  will be the case with all components in Zend. So instead of updating every file with include directive we autoload all the dependent files. 

autoload.php

namespace Zend;
spl_autoload_register(function ($class) {
    if (substr($class, 0, strlen(__NAMESPACE__)) != __NAMESPACE__) {
        //Only autoload libraries from this package
        return;
    }
    $path = substr(str_replace('\\', '/', $class), 0);
    $path = __DIR__ . "/".$path . '.php';
    if (file_exists($path)) {
        require $path;
    }

});

The main purpose of this article is to explain how to use ZF2 modules along with Codeigniter. Hope this article was helpfull. If you find this information incorrect please don't hesitate to comment. There is also an alternative by using the third_party option in Codeigniter. 


Wednesday, October 9, 2013

Installing zend framework 2 in ubuntu

There are a lot of resources available on Zend framework, but almost all of them are focused for experienced developers. I am writing this article so that a beginner can install the new Zend Framework 2 and start experimenting on it. The following are the steps that will let you achieve this:
Step 1: Create a directory under your web root. Lets say the folder name is "MyZendProject".
Step 2: Open Terminal and select the newly created directory(eg: cd /var/www/MyZendProject).
Step 3: Now type the following command in terminal
git clone git://github.com/zendframework/ZendSkeletonApplication.git and press enter.
This will install the Zend frameworks skeleton application in your computer. If git is not present, then you can simply install git by typing the following command in terminal
sudo apt-get install git and press enter.

Step 4:Once the skeleton application is downloaded in your folder you will find the composer.phar php file inside ZendSkeletonApplication folder. Just copy all the files inside this newly created folder to your folder. i.e from MyZendProject/ZendSkeletonApplication toMyZendProject. Our next job is to run the composer.phar file.

Step 5:In terminal type php composer.phar self-update and press enter.
Step 6: In terminal type php composer.pharinstall and press enter.
That's it. it's all done. Now open your browser and navigate to your http://localhost/MyZendProject/public/.
You can also download the Zend documentation from here

Please don't hesitate to comment if any problems occurs while performing these steps. Enjoy!!!.